Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获得地理编码器&x2019;在LatLng和x2019上的结果;美国的国家语言?_Java_Android_Locale_Reverse Geocoding - Fatal编程技术网

Java 如何获得地理编码器&x2019;在LatLng和x2019上的结果;美国的国家语言?

Java 如何获得地理编码器&x2019;在LatLng和x2019上的结果;美国的国家语言?,java,android,locale,reverse-geocoding,Java,Android,Locale,Reverse Geocoding,我在应用程序中使用反向地理编码将LatLng对象转换为字符串地址。我必须得到的结果不是设备的默认语言,而是给定地点所在国家的语言。有办法做到这一点吗? 这是我的密码: Geocoder geocoder = new Geocoder(context, Locale.getDefault()); List addresses; try { addresses = geocoder.getFromLocation(location.latitude, locatio

我在应用程序中使用反向地理编码将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 Geocoder=新的地理编码器(上下文,Locale.getDefault()); 列出地址; 试一试{ addresses=geocoder.getFromLocation(location.latitude,location.longitude,1); } 捕获(IOException | IndexOutOfBoundsException | NullPointerException ex){ 地址=空; } 返回地址;
在您的代码中,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;

这对我有用,希望对你也有用

你的意思是,如果这个设备是英文的,而且你在看北京,它实际上应该返回中文字符吗?@TeoInke不确定中文字符,我的意思是它应该返回法国的名字,德国的德语,意大利的意大利语,等等,明白了。从我发现的情况来看,可以在JS API上设置语言,但在Android上不可以。谢谢,这对我来说也很有效,只是我没有创建新的区域设置并使用步骤2中的对象。