【问题标题】:How can I get place details from Google Places API for Android?如何从适用于 Android 的 Google Places API 获取地点详细信息?
【发布时间】:2015-11-18 14:02:56
【问题描述】:

我想从自动完成地点服务获得的预测中获取地点的详细信息(城市名称、邮政编码等)。我的代码如下:

    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();
            }
        });

【问题讨论】:

  • 您可能可以使用地点 ID 来检索坐标,并使用坐标来检索城市名称。更多详情可以看this StackOveflow answer
  • 我已经看过那篇文章,但我对更直接的方式感兴趣。感谢您的回答! :)
  • 你找到更直接的方法了吗?
  • 还没有,我分两步获取地点详细信息..
  • 目前,您至少需要执行两个请求。请对此功能请求进行投票 - issuetracker.google.com/issues/35828699 以通过单个请求完成。

标签: android google-maps google-places-api


【解决方案1】:

您需要Geocode 地点结果才能取回该信息。


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();
        }
    });

【讨论】:

  • 如何获取placeId?
猜你喜欢
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 2012-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-30
  • 2012-03-20
相关资源
最近更新 更多