Google Places API Android resultCallback not firing

穿精又带淫゛_ 提交于 2019-12-20 04:07:26

问题


I'm using the Google Places API for Android and I can't seem to get a resultCallback from a PendingResult to fire. Here's my code (essentially identical to the sample code to https://github.com/googlesamples/android-play-places/tree/master/PlaceComplete except in a Dialog)

adapter = ((MainActivity) getActivity()).getAdapter();
googleApiClient = ((MainActivity) getActivity()).getGoogleApiClient();

final AutoCompleteTextView tvCity = new AutoCompleteTextView(getActivity());
tvCity.setAdapter(adapter);

tvCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final PlacesAutoCompleteAdapter.PlaceAutocomplete item = adapter.getItem(position);
        final String placeId = String.valueOf(item.placeId);

        PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
                .getPlaceById(googleApiClient, placeId);
        placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
            @Override
            public void onResult(PlaceBuffer places) {

            if (places.getStatus().isSuccess()) {
                    final Place place = places.get(0);
                    cityCoordinates = place.getLatLng();

                } else {
                    dialog.dismiss();
                    Toast.makeText(getActivity(), "There was an error, please try again", Toast.LENGTH_SHORT).show();
                    return;

                }
                dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
                places.release();
            }
        });
    }
});

回答1:


I had the same issue because I was missing the permission that allows the API to access Google web-based services

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



回答2:


For reference, to anyone who visits this question, make sure you do all 3 steps below:

Add your API key to your app manifest

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

Then include in your manifest as @Alex Baker suggested

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

And also enable Google Places API for Android. Google Places API is a different API and your calls will fail with Places AutoComplete status 9001



来源:https://stackoverflow.com/questions/29744045/google-places-api-android-resultcallback-not-firing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!