android位置服务 - 从用户当前位置确定国家/地区区域

时间:2011-09-03 22:35:54

标签: android google-maps gps

我正在编写一个使用位置服务的Android应用。我想确定用户所在的国家/地区。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

您可以使用Google Geocoder API

http://code.google.com/apis/maps/documentation/geocoding/

您需要做的就是进行适当的查询,例如在Google Geocoder的请求中提供用户在纬度和经度格式(在网址中)的位置,然后解析JSON / XML响应。您可以看到,在提供的示例中,有一个表示国家/地区的JSON字符串:

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    }, {
      "long_name": "Amphitheatre Pkwy",
      "short_name": "Amphitheatre Pkwy",
      "types": [ "route" ]
    }, {
      "long_name": "Mountain View",
      "short_name": "Mountain View",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "California",
      "short_name": "CA",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",    // <----- HERE IS THE COUNTRY NAME.
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "94043",
      "short_name": "94043",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 37.4219720,
        "lng": -122.0841430
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 37.4188244,
          "lng": -122.0872906
        },
        "northeast": {
          "lat": 37.4251196,
          "lng": -122.0809954
        }
      }
    }
  } ]
}

如果我不清楚或没有链接,你可以阅读更多。或者使用Google Maps API中的Geocoder类for android