【发布时间】:2012-06-06 16:32:52
【问题描述】:
我是 android 移动开发的新手。我已经使用了 Location Manager 类并成功地找出了用户的经度和纬度。我想使用这些值来查找城市名称。我不想要地图,我只想得到城市名称。我该怎么做呢?
【问题讨论】:
我是 android 移动开发的新手。我已经使用了 Location Manager 类并成功地找出了用户的经度和纬度。我想使用这些值来查找城市名称。我不想要地图,我只想得到城市名称。我该怎么做呢?
【问题讨论】:
首先使用 Location 和 LocationManager 类(您已完成)获取纬度和经度。现在试试下面的代码获取城市,地址信息
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
城市信息现在是 sb。现在将 sb 转换为 String(使用 sb.toString() )。
【讨论】:
【讨论】:
Location Manager 用于检索经纬度,但该人并没有真正询问它。该问题表明答案是 Reverse Geo-coding 。建议您在开始获得反对票之前编辑您的答案..
您可以使用Geocoder
Geocoder myLocation = new Geocoder(context, Locale.getDefault());
List<Address> myList = null;
try {
myList = myLocation.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {}
经度和纬度是网络或 GPS 检索到的值
【讨论】: