Fail to obtain specific Place from PlaceBuffer
Asked Answered
P

4

10

I have strange problem with one specific place. Any idea ?

This code is working fine, but for one place (accidentally choosen because of starts with aaa - just for test) it fails to obtain Place object from buffer. Even a query was successfull (log: PlaceAutocompleteAdapt﹕ Query completed. Received 5 predictions.) Unfortunatelly, it crash application. It make this API very dangerous, what else should be handled in try catch block for sure?

        addressAutocompleteText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            /*
             Retrieve the place ID of the selected item from the Adapter.
             The adapter stores each Place suggestion in a PlaceAutocomplete object from which we
             read the place ID.
              */
            final PlaceAutocompleteAdapter.PlaceAutocomplete item = ((MainActivity) getActivity()).mAdapter.getItem(position);
            final String placeId = String.valueOf(item.placeId);
            Log.i(TAG, "Autocomplete item selected: " + item.description);

            /* Issue a request to the Places Geo Data API to retrieve a Place object with additional details about the place. */
            PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi.getPlaceById(((MainActivity) getActivity()).mGoogleApiClient, placeId);
            placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
                @Override
                public void onResult(PlaceBuffer places) {
                    if (!places.getStatus().isSuccess()) {
                        // Request did not complete successfully
                        Log.e(TAG, "Place query did not complete. Error: " + places.getStatus().toString());
                        places.release();
                        return;
                    }

                    try {
                        // Get the Place object from the buffer.
                        final Place place = places.get(0); // Throws IllegalStateException
                        if (null != place) {
                            coordinate = new Coordinate(place.getLatLng().latitude, place.getLatLng().longitude);
                        }
                    } catch (IllegalStateException e) {
                        Log.w(TAG, "Fail to get place or its coordinates");
                    }

                    places.release();
                }
            });
//                hideKeyboard(addressAutocompleteText);

            Log.i(TAG, "Called getPlaceById to get Place details for " + item.placeId);
        }
    });

LOG:

Starting autocomplete query for: aaa
06-05 08:05:29.618    6403-7777/...I/PlaceAutocompleteAdapt﹕ Query completed. Received 5 predictions.
06-05 08:05:39.248    6403-6403/...I/FavoriteLocationCreateFragment﹕ Autocomplete item selected: AAA Auto A.s., Zlín, Česká republika
06-05 08:06:01.278    6403-6403/...I/FavoriteLocationCreateFragment﹕ Called getPlaceById to get Place details for ChIJcYm-YVpzE0cR-OIfKj2KTKc
06-05 08:30:46.823  19727-19727/... D/AndroidRuntime﹕ Shutting down VM
06-05 08:30:46.833  19727-19727/... E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: ..., PID: 19727
    java.lang.IllegalStateException
            at com.google.android.gms.common.internal.zzv.zzP(Unknown Source)
            at com.google.android.gms.common.data.zzc.zzaB(Unknown Source)
            at com.google.android.gms.common.data.zzc.<init>(Unknown Source)
            at com.google.android.gms.location.places.internal.zzq.<init>(Unknown Source)
            at com.google.android.gms.location.places.internal.zzo.<init>(Unknown Source)
            at com.google.android.gms.location.places.PlaceBuffer.get(Unknown Source)
            at ....FavoriteLocationCreateFragment$3$1.onResult(FavoriteLocationCreateFragment.java:129)
            at ....FavoriteLocationCreateFragment$3$1.onResult(FavoriteLocationCreateFragment.java:117)
            at com.google.android.gms.common.api.AbstractPendingResult$CallbackHandler.deliverResultCallback(Unknown Source)
            at com.google.android.gms.common.api.AbstractPendingResult$CallbackHandler.handleMessage(Unknown Source)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Pelagianism answered 5/6, 2015 at 6:33 Comment(5)
What is PlaceAutocomplete? It is possible you are getting bad IDs from there. I tried testing the ID from your log message "ChIJcYm-YVpzE0cR-OIfKj2KTKc", and I do not get any results back. It seems like there is no place with that ID. In that case, the API gives you a place buffer with zero results inside it. If you call places.get(0) you are trying to retrieve something that does not exist. The problem with this theory is that places.getStatus().isSuccess() should return false in that case, which you already check for. Consider logging places.getCount() to aid in debugging.Hertzfeld
PlaceAutocomplete is API from google developers.google.com/places/android/autocomplete Yes, placeID returns empty result. But why I got it in first place? When status is SUCCESS and count is 5 (log: Received 5 predictions.). How is possible that buffer is empty? It seems like a bug )-:Pelagianism
make sure to add .addApi(Places.GEO_DATA_API) to GoogleApiClientWeb
I have the same nasty bug. I am sure it s on Google's code.Lisandra
any solution beside try catch?Arsenical
H
7

Add condition && places.getCount() > 0

Example from documentation:

Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId)
                 .setResultCallback(new ResultCallback<PlaceBuffer>() {
        @Override
        public void onResult(PlaceBuffer places) {
            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();
    }
});
Hideous answered 9/8, 2016 at 20:45 Comment(0)
N
2

Need places.getCount() check as well. Please refer here https://developers.google.com/places/android-api/place-details

Navigable answered 8/7, 2016 at 10:7 Comment(0)
E
1

I am also having this same problem.

I get Place ID: ChIJD1eejrW35zsRDFfkT-PSbm4 from item.getPlaceId() method, but when I do Reverse Geocoding by Place ID, I do not get any results.

There may be many places that returns the same result as this, so I hope Google recognizes this issue.

But for now I have done this:

/**
 * Callback for results from a Places Geo Data API query that shows the first place result in
 * the details view on screen.
 */
private ResultCallback<PlaceBuffer> fromPlaceResultCallback = new ResultCallback<PlaceBuffer>() {
    @Override
    public void onResult(@NonNull PlaceBuffer places) {
        if (!places.getStatus().isSuccess()) {
            places.release();
            return;
        }
        // Get the Place object from the buffer.
        if(places.getCount() > 0) {
            // Got place details
            Place place = places.get(0);
            // Do your stuff
        } else {
            // No place details 
            Toast.makeText(SelectLocation.this, "Place details not found.", Toast.LENGTH_LONG).show();
        }
        places.release();
    }
};
Exhibitive answered 1/8, 2016 at 21:7 Comment(0)
D
-1

This happened to me if placeId was wrong/invalid in the call

Places.GeoDataApi.getPlaceById(((MainActivity) getActivity()).mGoogleApiClient, placeId);
Diagenesis answered 11/10, 2015 at 23:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.