Android Google Places API, getAutocompletePredicti

2019-01-14 15:45发布

I'm having problems with Android Google Places API - auto complete feature. I use the same key that i used for Android Google Maps API (and in the documentation, it is written this is ok). Here is my definition in manifest:

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="mykey"/>

But getAutocompletePredictions returns 'PLACES_API_ACCESS_NOT_CONFIGURED' message as status.

Here is my Java code:

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

LatLngBounds latLngBounds = new LatLngBounds.Builder().
        include(SphericalUtil.computeOffset(latlon, RADIUS, 0)).
        include(SphericalUtil.computeOffset(latlon, RADIUS, 90)).
        include(SphericalUtil.computeOffset(latlon, RADIUS, 180)).
        include(SphericalUtil.computeOffset(latlon, RADIUS, 270)).build();

PendingResult<AutocompletePredictionBuffer> result = Places.GeoDataApi.getAutocompletePredictions(googleApiClient, constraint.toString(), latLngBounds, null);

AutocompletePredictionBuffer autocompletePredictions = result.await(Config.DATA_TIMEOUT, TimeUnit.MILLISECONDS);

Status status = autocompletePredictions.getStatus();
if (status.isSuccess()) {
    Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
    while (iterator.hasNext()) {
        AutocompletePrediction prediction = iterator.next();
        //... do stuff here ...
    }
}
else {
    Log.d(TAG, status.getStatusMessage());
}

autocompletePredictions.release();
googleApiClient.disconnect();

Thanks in advance

4条回答
爷的心禁止访问
2楼-- · 2019-01-14 16:20

Step by step Solution that I follows :

enter image description here

查看更多
够拽才男人
3楼-- · 2019-01-14 16:29

I think you tried enabling Google Places API for Android

Enable this as well it works . You can found this below Google Places API for Android

Google Places API Web Service
查看更多
虎瘦雄心在
4楼-- · 2019-01-14 16:33

googleApiClient.connect(); should be called in the method onMapReady of the interface OnMapReadyCallback.

查看更多
冷血范
5楼-- · 2019-01-14 16:40

Enable the Google Places API for Android in developers console and check on the credentials page that your key is still present

查看更多
登录 后发表回答