[英]get the country name from Google geocode reverse API, android
我想获得给定纬度和经度的国家/地区名称。 我正在使用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
}
}
}
},
如何从此结果中获取国家/地区名称。
使用新的Location API来执行此操作。 这是相同的教程-https: //developer.android.com/training/location/display-address.html 。 对于国家名称,只需在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();
}
(要么)
尝试与此
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.