Swapnil Mankar
Swapnil Mankar

Reputation: 21

Google places getPlaceById callback not getting invoked

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();
        }
    });

Upvotes: 0

Views: 1447

Answers (2)

Davion
Davion

Reputation: 16

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.

Upvotes: 0

Swapnil Mankar
Swapnil Mankar

Reputation: 21

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

Upvotes: 2

Related Questions