如何从Android版Google Places API获取地点详细信息? [英] How can I get place details from Google Places API for Android?

查看:187
本文介绍了如何从Android版Google Places API获取地点详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从自动完成地点服务获得的预测中获取地点的详细信息(cityName,ZipCode等).我的代码如下:

I want to get details of places (cityName, ZipCode, etc) from predictions that I get from Autocomplete Places service. My code is like the following:

    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查找地点详细信息,我将无法获得既不需要的信息:

Is this possible? How can I implement it? If I look for place details by placeId, I can't get that I want neither:

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

推荐答案

您需要Geocode地点结果以获取该信息.

You'll need to Geocode the place results to get back that information.

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

这篇关于如何从Android版Google Places API获取地点详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆