Google Places API Android resultCallback未触发
问题描述:
我正在使用Google Places API for Android,我似乎无法从 PendingResult $ c $获取
resultCallback
c>开火。这是我的代码(基本上与 https:// github上的代码示例相同。 com / googlesamples / android-play-places / tree / master / PlaceComplete 除了在对话框中)
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();
}
});
}
});
答
我有同样的问题,因为我缺少权限允许API访问Google基于网络的服务
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"/>