如何在LatLng的国家语言上获得Geocoder的结果



我在应用程序中使用反向地理编码将LatLng对象转换为字符串地址。我必须得到它的结果不是在设备的默认语言,而是在给定位置的国家的语言。有办法做到这一点吗?这是我的代码:

Geocoder Geocoder=新的Geocoder(context,Locale.getDefault());列出地址;尝试{addresses=geocoder.getFromLocation(location.latitude,location.longitude,1);}catch(IOException | IndexOutOfBoundsException | NullPointerException ex){addresses=null;}返回地址;

在您的代码中,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和Geocoder,然后再次请求。

    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;

这对我有效,希望对你也有效!

最新更新