如何从谷歌地图中的经纬度坐标获取城市名称?
问题描述:
如果我有一个城镇或地区的经纬度坐标,我如何在 Google 地图中获取城市名称?
How might I obtain the city name in Google Maps if I have latitude and longitude coordinates of a town or area?
我尝试使用纬度、经度并获得国家/地区,但我不知道如何获得城市名称.
I tried using the latitude, longitude and I got country but I don't know how to get city name.
答
来自 Geocoder
对象,可以调用getFromLocation(double, double, int)
方法.它将返回 Address
对象的列表有一个方法 getLocality()
.
Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0) {
System.out.println(addresses.get(0).getLocality());
}
else {
// do your stuff
}