android location services - determine country regi

2019-07-25 12:04发布

I'm writing an Android app, which uses location services. I want to detrmine the country region, in which the user is located. How can I achieve that ?

1条回答
甜甜的少女心
2楼-- · 2019-07-25 12:35

You can make a use of the Google Geocoder API

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

All you need to do is to make appropriate query, e.g in the request to the Google Geocoder providing the user's location in Latitude and Longitude format (in the url), and then parse the JSON/XML response. You can see that in the example that is provided there is JSON string that signifies the country:

{
  "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
        }
      }
    }
  } ]
}

If I was unclear or short on the link you can read more. Or use Geocoder class from Google Maps API for android

查看更多
登录 后发表回答