首页 > 解决方案 > Google Place API SDK 国家/地区代码 - 如何?

问题描述

使用 google Place API Android SDK。无论如何从地点对象中检索国家名称/国家代码?

它在 IOS sdk 中的 address_components 中确实有,但在 Android SDK 中找不到相同的元素。

任何想法?

标签: androidgoogle-places-api

解决方案


https://maps.googleapis.com/maps/api/geocode/json?latlng=lati,longi&key=myApiKey


如果您正在寻找详细信息,您也可以从 Google Place API 检查下面的 API 解析 JSON ,

https://developers.google.com/places/web-service/details https://developers.google.com/places/web-service/autocomplete

或者

查找以下代码以获取当前国家/地区或通过纬度和经度获取国家/地区。

        public static String getCountryName(Context context, double latitude, double longitude) {
            Geocoder geocoder = new Geocoder(context, Locale.getDefault());
            List<Address> addresses;
            try {
                addresses = geocoder.getFromLocation(latitude, longitude, 1);
                if (addresses != null && !addresses.isEmpty()) {
                    return addresses.get(0).getCountryName();
                }
            } catch (IOException ioe) { }
            return null;
        }


  [1]: http://

推荐阅读