简体   繁体   English

从Google Geocode反向API获取国家名称,Android

[英]get the country name from Google geocode reverse API, android

I want to get the country name of a given latitude and longitude. 我想获得给定纬度和经度的国家/地区名称。 I am using Google Geocode API. 我正在使用Google Geocode API。 I am using the following query : 我正在使用以下查询:

"http://maps.googleapis.com/maps/api/geocode/json?latlng="+lat1+","+lng1+"&sensor=true_or_false"

the result of the JSON is like this: JSON的结果如下:

{
  "status": "OK",
  "results": [ {
    "types": street_address,
    "formatted_address": "275-291 Bedford Ave, Brooklyn, NY 11211, USA",
    "address_components": [ {
      "long_name": "275-291",
      "short_name": "275-291",
      "types": street_number
    }, {
      "long_name": "Bedford Ave",
      "short_name": "Bedford Ave",
      "types": route
    }, {
      "long_name": "New York",
      "short_name": "New York",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Brooklyn",
      "short_name": "Brooklyn",
      "types": [ "administrative_area_level_3", "political" ]
    }, {
      "long_name": "Kings",
      "short_name": "Kings",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "New York",
      "short_name": "NY",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "11211",
      "short_name": "11211",
      "types": postal_code
    } ],
    "geometry": {
      "location": {
        "lat": 40.7142298,
        "lng": -73.9614669
      },
      "location_type": "RANGE_INTERPOLATED",
      "viewport": {
        "southwest": {
          "lat": 40.7110822,
          "lng": -73.9646145
        },
        "northeast": {
          "lat": 40.7173774,
          "lng": -73.9583193
        }
      }
    }
  },

how to get the country name from this result. 如何从此结果中获取国家/地区名称。

Use the new Location API to do this. 使用新的Location API来执行此操作。 Here's a tutorial for the same - https://developer.android.com/training/location/display-address.html . 这是相同的教程-https: //developer.android.com/training/location/display-address.html For the country name, just call getCountryName() on the Address object. 对于国家名称,只需在Address对象上调用getCountryName()

Geocoder gCoder = new Geocoder(this);
            List<Address> addresses;
            try {
                addresses = gCoder.getFromLocation(clat, clng, 5);
                Log.e("get adderess", addresses + "");
                if (addresses != null && addresses.size() > 0) {
                    addres = addresses.get(0).getLocality();
                    System.out.print(addresses.get(0).getLocality());
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

(or) (要么)

try even with this 尝试与此

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在谷歌地图 api 2 android 中反转地理编码 - How to reverse Geocode in google maps api 2 android 如何在Android中使用反向地理编码获取名称海洋? - how to get name ocean with reverse geocode in android? 我想解析Android Map中的反向地理编码的Google Map API - I want to parse Google Map API for reverse geocode in Android 从android中的国家名称获取国家/地区代码 - get country code from country name in android 从 PlaceAutocompleteFragment android (Google Places API) 获取国家代码 - Get country code from PlaceAutocompleteFragment android (Google Places API) 从PlaceAutocomplete android获取国家代码(Google Places API) - Get country code from PlaceAutocomplete android (Google Places API) 从Google API地理编码获取正确的重点-Android - Getting the right accentuation from Google API Geocode - Android 有没有办法从android中的国家名称获取国家资本? - Is there a way to get country capital from country name in android? 如何通过Google登录API获取用户国家/地区名称? - How to get user country name by google signin API? 如何在Google Places API中针对Android中的自动完成预测获取特定国家/地区的结果? - How to get Country Specific Results in Google Places API for AutocompletePredictions in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM