无法使用PlaceDetectionClient获取位置-Android

时间:2018-12-19 10:28:37

标签: android android-studio google-places-api google-places

我正在尝试使用PlaceDetectionClient获取当前地址。

但是我收到此错误。

com.google.android.gms.common.api.ApiException: 13: ERROR
        at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(Unknown Source)
        at com.google.android.gms.common.internal.zai.zaf(Unknown Source)
        at com.google.android.gms.common.internal.zaj.onComplete(Unknown Source)
        at com.google.android.gms.common.api.internal.BasePendingResult.zaa(Unknown Source)
        at com.google.android.gms.common.api.internal.BasePendingResult.setResult(Unknown Source)
        at com.google.android.gms.location.places.zzm.zzb(Unknown Source)
        at com.google.android.gms.location.places.internal.zzz.dispatchTransaction(Unknown Source)
        at com.google.android.gms.internal.places.zzc.onTransact(Unknown Source)
        at android.os.Binder.execTransact(Binder.java:570)

这是我的代码:

 mPlaceDetectionClient = Places.getPlaceDetectionClient(this);

 private void showCurrentPlace() {
    if (mGoogleMap == null) return;

    if (mLocationPermissionGranted) {          
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED)
            return;
        Task<PlaceLikelihoodBufferResponse> placeResult = mPlaceDetectionClient.getCurrentPlace(null);
        placeResult.addOnCompleteListener(new OnCompleteListener<PlaceLikelihoodBufferResponse>() {
            @Override
            public void onComplete(@NonNull Task<PlaceLikelihoodBufferResponse> task) {
                if (task.isSuccessful() && task.getResult() != null) {
                    PlaceLikelihoodBufferResponse likelyPlaces = task.getResult();
                    if (likelyPlaces.getCount() > 0) {
                        sourceTxt.setText(likelyPlaces.get(0).getPlace().getAddress());                          
                    }
                    likelyPlaces.release();
                } else Log.e("MainActivity", "RRR Exception: %s", task.getException());
            }
        });           
    } else getLocationPermission();
}

还有我的gradle依赖项...

implementation 'com.google.android.gms:play-services-location:16.0.0'

implementation 'com.google.android.gms:play-services-places:16.0.0'

1 个答案:

答案 0 :(得分:1)

Please check in the manifest you added the following things:

<meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <!--
         The API key for Google Maps-based APIs.
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="your_map_api_key" />

and also check whether you are running in a real device or emulator.