Google 地方 API Android resultCallback 未触发

Google Places API Android resultCallback not firing

我正在为 Android 使用 Google Places API,但我似乎无法从 PendingResult 中获取 resultCallback 来触发.这是我的代码(与 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();
            }
        });
    }
});

我遇到了同样的问题,因为我缺少允许 API 访问 Google 基于 Web 的服务的权限

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

作为参考,访问此问题的任何人,请务必执行以下所有 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 位置 API。 Google 地方 API 是不同的 API,您的呼叫将失败