0

I am building an android app that saves a place ID retrieved from the PlaceAutocomplete API. At a later point, I am trying to get the details of the place using the getPlaceById() API. I see that the callback is never getting called.

I have set the following permission:

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

I have also added the API_KEY:

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

However, I am unable to retrieve the place details. "onResult" never seems to be getting called. Can anyone please help me with where I might be going wrong?

Thanks!

Below is the code snippet that I am using. Have hardcoded the PlaceId here for simplicity :

    PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi.getPlaceById(mGoogleApiClient, "ChIJi-t8KwUWrjsRlp-L9ykb2_k");
    placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
        @Override
        public void onResult(PlaceBuffer places) {
            Log.i(TAG, "Testing");
            if (places.getStatus().isSuccess() && places.getCount() > 0) {
                final Place myPlace = places.get(0);
                Log.i(TAG, "Place found: " + myPlace.getName());
            } else {
                Log.e(TAG, "Place not found");
            }
            places.release();
        }
    });
Kapil Rajput
  • 11,429
  • 9
  • 50
  • 65
  • can you share your code how you are building request of GoogleApiClient ? – Kapil Rajput Jan 11 '16 at 09:35
  • @kapilrajput Here is how I am building the request: mGoogleApiClient = new GoogleApiClient .Builder(this) .addApi(Places.GEO_DATA_API) .addApi(Places.PLACE_DETECTION_API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); – Swapnil Mankar Jan 11 '16 at 09:38
  • @kapilrajput Thanks for your help :) – Swapnil Mankar Jan 11 '16 at 10:02
  • Note you shouldn't need the READ_GSERVICES permission to use this API. Did you copy this from a sample somewhere? – Daniel Resnick Jan 12 '16 at 10:39
  • @DanielResnick You are right. I removed the permission and it still works fine. I had initially referred to the answer here (http://stackoverflow.com/questions/29744045/google-places-api-android-resultcallback-not-firing) which suggested that the permission was needed. – Swapnil Mankar Jan 13 '16 at 12:07

2 Answers2

2

I just found out what I was missing out. I missed out the call to mGoogleApiClient.connect(); in the onStart() of the activity. Works like a charm now! :)

The comment in the onCreate() in the below link states that we need to call connect() and disconnect() explicitly if the activity does not extend FragmentActivity.

https://github.com/tangqi92/MyGooglePlaces/blob/master/app/src/main/java/itangqi/me/mygoogleplaces/MainActivity.java

0

I got the same problem. And actually it's not "not getting invoked" but "haven't run yet". Here is my wrong code.

public void onClick(View v) {
    hideSoftKeyboard();
    Log.i("Search Click", "getting Place: " + mMyLocation.id);
    if(mMyLocation.id != null) {
        Places.GeoDataApi.getPlaceById(mGoogleApiClient, mMyLocation.id)
                .setResultCallback(new ResultCallback<PlaceBuffer>() {
                    @Override
                    public void onResult(PlaceBuffer places) {
                        if (places.getStatus().isSuccess() && places.getCount() > 0) {
                            LatLng coord = places.get(0).getLatLng();
                            mMyLocation.setLatLng(coord.latitude, coord.longitude);
                            Log.i("Place by id", "Place found: " + mMyLocation.coordinateString);
                        } else {
                            Log.e("Place by id", "Place not found");
                        }
                        places.release();

                    }
                });
    searchNearby();
    }
}

The searchNearby()function uses mMyLocation that should have been changed in onResult. And it hasn't been changed yet, which means onResult hasn't been called before the searchNearby() run. Then I put the function searchNearby() into onResult and it worked.

So my suggestion would be: put anything you want to run after onResult into it.

Davion
  • 16
  • 2