[英]How to get geocoded address from latitude, longitude?
我有以下代码将经纬度转换为人类可读的地址。 现在,我需要一些时间来获取地址的完整详细信息,包括地址,城市,邮政编码以及一些时间只能获得诸如“印度”之类的县名。 如何获得基于纬度和经度的准确地址? 请帮我。
final List<Address> list = gCoder.getFromLocation(locationLatitude, locationLongitude, 1);
if (list != null && list.size() > 0) {
Address address = list.get(0);
StringBuilder sb = new StringBuilder();
if (address.getMaxAddressLineIndex() > 0) {
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append(",");
}
sb.append(address.getCountryName());
} else {
try {
sb.append(address.getAddressLine(0));
} catch (Exception ignored) {
}
}
strAddress = sb.toString();
strAddress = strAddress.replace(" ", "");
strAddress = strAddress.replace(",null", "");
strAddress = strAddress.replace("null", "");
strAddress = strAddress.replace("Unnamed", "");
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (!TextUtils.isEmpty(strAddress)) {
tvLocation.setText(strAddress);
Log.e("Location", strAddress + "");
}else {
Snackbar.make(getView(), "Couldn't get the location. Make sure location is enabled on the device", Snackbar.LENGTH_SHORT)
.show();
}
}
});
您可以从Geocoder
对象中调用getFromLocation(double, double, int)
方法。
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.