[英]How to get Geocoder’s results on the LatLng’s country language?
我在应用程序中使用反向地理编码将LatLng对象转换为字符串地址。 我得得到的结果不是使用设备的默认语言,而是使用确定给定位置的国家/地区的语言。 有没有办法做到这一点? 这是我的代码:
Geocoder geocoder = new Geocoder(context, Locale.getDefault()); List addresses; try { addresses = geocoder.getFromLocation(location.latitude, location.longitude, 1); } catch (IOException | IndexOutOfBoundsException | NullPointerException ex) { addresses = null; } return addresses;
在您的代码中,Geocoder以设备语言环境(语言)返回地址文本。
1从“地址”列表的第一个元素中,获取国家/地区代码。
Address address = addresses.get(0);
String countryCode = address.getCountryCode
然后返回国家代码(例如“ MX”)
2获取国家名称。
String langCode = null;
Locale[] locales = Locale.getAvailableLocales();
for (Locale localeIn : locales) {
if (countryCode.equalsIgnoreCase(localeIn.getCountry())) {
langCode = localeIn.getLanguage();
break;
}
}
3再次实例化区域设置和地理编码器,然后再次请求。
Locale locale = new Locale(langCode, countryCode);
geocoder = new Geocoder(this, locale);
List addresses;
try {
addresses = geocoder.getFromLocation(location.latitude, location.longitude, 1);
}
catch (IOException | IndexOutOfBoundsException | NullPointerException ex) {
addresses = null;
}
return addresses;
这对我有用,希望对您也有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.