【问题标题】:get the country name from Google geocode reverse API, android从 Google geocode reverse API, android 获取国家名称
【发布时间】:2013-12-02 12:00:43
【问题描述】:

我想获取给定纬度和经度的国家名称。我正在使用谷歌地理编码 API。我正在使用以下查询:

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

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
        }
      }
    }
  },

如何从这个结果中得到国家名称。

【问题讨论】:

    标签: android google-maps geocoding


    【解决方案1】:

    使用新的 Location API 来执行此操作。这是相同的教程 - https://developer.android.com/training/location/display-address.html。对于国家名称,只需在 Address 对象上调用 getCountryName()

    【讨论】:

    • 我以前用过,但在我的国家不起作用,所以我想改用 Google Geocode API
    • 在这种情况下,您将不得不做一些肮脏的事情。使用 GSON 之类的解析 JSON 并通过解析 formatted_address 值的值来获取名称
    • 我从未听说过 GSON :( 我怎么能在这个查询中使用它
    • GSON 是 Google 的一个库,用于序列化/反序列化 JSON (code.google.com/p/google-gson)。见前sites.google.com/site/gson/gson-user-guide#TOC-Object-Examples
    【解决方案2】:
    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();
                }
    

    (或)

    试试这个

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

    【讨论】:

    • 我以前用过,但在我的国家不起作用,所以我想改用 Google Geocode API
    • 我已经更新了代码...你可以试试这个..with Geocod
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多