我正在使用Google Places API for Android,而我似乎无法从resultCallback
获取PendingResult
。这是我的代码(基本上与https://github.com/googlesamples/android-play-places/tree/master/PlaceComplete的示例代码相同,除了在对话框中)
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();
}
});
}
});
答案 0 :(得分:4)
我遇到了同样的问题,因为我错过了允许API访问Google基于网络的服务的权限
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
答案 1 :(得分:1)
作为参考,对于访问此问题的任何人,请确保执行以下所有3个步骤:
将API密钥添加到应用清单
<application>
...
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
</application>
然后在你的清单中加入@Alex Baker建议
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
并启用适用于Android的Google Places API 。 Google Places API是一种不同的API,您的来电将因Places AutoComplete status 9001
而失败