如何从Google Places API for Android获取地点详情?

3

我希望从Autocomplete Places服务中获取预测结果的位置细节(城市名称、邮政编码等)。我的代码如下:

    Places.GeoDataApi.getAutocompletePredictions(googleApiClient, query, bounds, null)
            .setResultCallback(
                    new ResultCallback<AutocompletePredictionBuffer>() {
                        @Override
                        public void onResult(AutocompletePredictionBuffer buffer) {
                            if (buffer == null)
                                return;

                            if (buffer.getStatus().isSuccess()) {
                                for (AutocompletePrediction prediction : buffer) {
                                    // How to get cityName here
                                }
                            }
                            buffer.release();
                        }
                    }, 15, TimeUnit.SECONDS);

这个可行吗?我该如何实现它? 如果我通过placeId查找地点详情,我也得不到我想要的:

Places.GeoDataApi.getPlaceById(googleApiClient, placeId)
        .setResultCallback(new ResultCallback<PlaceBuffer>() {
            @Override
            public void onResult(PlaceBuffer places) {
                if (places.getStatus().isSuccess()) {
                    // How to get cityName here
                }
                places.release();
            }
        });

2
您可以使用地点ID来检索坐标,并使用坐标来检索城市名称。您可以查看此StackOveflow答案获取更多详细信息。 - ztan
我已经看过那篇帖子了,但我对更直接的方法感兴趣。谢谢你的回答! :) - NewUser
你有没有找到更直接的方法? - Darpan
还没有,我需要分两步获取地点的详细信息。 - NewUser
目前,您必须至少进行两个请求。请点赞此功能请求 - https://issuetracker.google.com/issues/35828699,以便使用单个请求完成。 - AlexKorovyansky
1个回答

5
你需要对地点结果进行地理编码才能获取相关信息。
Places.GeoDataApi.getPlaceById(googleApiClient, placeId)
    .setResultCallback(new ResultCallback<PlaceBuffer>() {

        @Override
        public void onResult(PlaceBuffer places) {

            if (!places.getStatus().isSuccess()) {
                // Request did not complete successfully
                return;
            }

            // Setup Geocoder
            Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
            List<Address> addresses;

            // Attempt to Geocode from place lat & long
            try {

                addresses = geocoder.getFromLocation(
                    place.getLatLng().latitude, 
                    place.getLatLng().longitude, 
                    1);

                if (addresses.size() > 0) {

                    // Here are some results you can geocode
                    String ZIP;
                    String city;
                    String state;
                    String country;

                    if (addresses.get(0).getPostalCode() != null) {
                        ZIP = addresses.get(0).getPostalCode();
                        Log.d("ZIP", ZIP);
                    }

                    if (addresses.get(0).getLocality() != null) {
                        city = addresses.get(0).getLocality();
                        Log.d("city", city);
                    }

                    if (addresses.get(0).getAdminArea() != null) {
                        state = addresses.get(0).getAdminArea();
                        Log.d("state", state);
                    }

                    if (addresses.get(0).getCountryName() != null) {
                        country = addresses.get(0).getCountryName();
                        Log.d("country", country);
                    }

                }

            } catch (IOException e) {
                e.printStackTrace();
            }

            places.release();
        }
    });

1
如何获取placeId? - Ashwin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接