我想获得给定纬度和经度的国家/地区名称。我正在使用Google Geocode 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
}
}
}
},
如何从此结果中获取国家/地区名称。
答案 0 :(得分:0)
使用新的Location API执行此操作。这是一个相同的教程 - https://developer.android.com/training/location/display-address.html。对于国家/地区名称,只需在getCountryName()
对象上调用Address
。
答案 1 :(得分:0)
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;
}