使用Google Place API获取当前位置

时间:2015-10-12 12:53:06

标签: android google-api location

我正在编写一个应用程序,尝试使用Google Place API获取当前位置。但是我得到了以下错误日志:

E/AndroidRuntime(20212): java.lang.IllegalArgumentException: GoogleApiClient is not configured to use the API required for this call.
E/AndroidRuntime(20212):    at com.google.android.gms.common.internal.zzx.zzb(Unknown Source)
E/AndroidRuntime(20212):    at com.google.android.gms.internal.zzli.zza(Unknown Source)
E/AndroidRuntime(20212):    at com.google.android.gms.location.places.internal.zzj.getCurrentPlace(Unknown Source)
E/AndroidRuntime(20212):    at ala.youngashram.droid.com.ala.ALaActivity.getCurrentLocation(ALaActivity.java:266)
E/AndroidRuntime(20212):    at ala.youngashram.droid.com.ala.ALaActivity.onConnected(ALaActivity.java:149)
E/AndroidRuntime(20212):    at com.google.android.gms.common.internal.zzk.zzh(Unknown Source)
E/AndroidRuntime(20212):    at com.google.android.gms.internal.zzlg.zznU(Unknown Source)
E/AndroidRuntime(20212):    at com.google.android.gms.internal.zzlg.onConnected(Unknown Source)
E/AndroidRuntime(20212):    at com.google.android.gms.internal.zzli$2.onConnected(Unknown Source)
E/AndroidRuntime(20212):    at com.google.android.gms.common.internal.zzj$zzg.zzpf(Unknown Source)
E/AndroidRuntime(20212):    at com.google.android.gms.common.internal.zzj$zza.zzc(Unknown Source)

任何建议我在这里缺少什么,我需要配置来获得这个!

更新了工作代码:感谢@ProblemSlover的分享link

mGoogleApiClient = new GoogleApiClient
                .Builder(this)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

这适用于连接请求:

mGoogleApiClient.connect();

以及获取位置后,请在成功连接后调用它:

PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
                .getCurrentPlace(mGoogleApiClient, null);

        result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
            @Override
            public void onResult(PlaceLikelihoodBuffer placeLikelihoods) {

                for (PlaceLikelihood placeLikelihood : placeLikelihoods) {
                    log(String.format("Place '%s' has likelihood: %g",
                            placeLikelihood.getPlace().getName(),
                            placeLikelihood.getLikelihood()));
                }

                placeLikelihoods.release();
            }
        });

2 个答案:

答案 0 :(得分:7)

请向我们展示您用于配置GoogleApiClient的代码。请确保按照&#34;连接到Places API&#34;部分中所述的正确方式完成操作。在入门页面上。 https://developers.google.com/places/android-api/start 这是配置剪辑:

// Your activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mGoogleApiClient = new GoogleApiClient
                .Builder(this)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

答案 1 :(得分:0)

将此方法添加到您的活动中,并在onCreate方法中调用它

private synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(new MapCallback())
            .addOnConnectionFailedListener(new FailedCallback())
            .addApi(LocationServices.API)
            .build();
}