【问题标题】:Get location name in the language of its country获取所在国家/地区语言的位置名称
【发布时间】:2018-03-04 18:48:47
【问题描述】:

我正在开发一个显示世界各地公园的 android 应用程序,但我遇到了下一个问题:

在我的应用程序中,我希望用户在地图上选择他正在创建的新公园所在的位置,并且我希望其他人无论身在何处都能看到他的公园。所以为了做到这一点,我需要确保公园的位置名称是公园所在国家/地区的语言。例如,如果公园位于希腊,则其位置名称将为希腊语。如果公园在美国,它的名称将是英文。 所以为了使它成为可能,我需要能够获得公园地址的语言环境。

为了获得公园的名称,我使用以下代码:

    public String getAddressName(Latlng latLng){
            Geocoder geocoder;
            List<Address> addresses;
            geocoder = new Geocoder(this, Locale.getDefault());

            try{
                addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
                geocoder = new Geocoder(this, addresses.get(0).getLocale());
                addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
                if (addresses.size() > 0)
                    return addresses.get(0).getAddressLine(0);
            }
            return null;
    }

问题在于此代码以手机默认语言返回位置。 为了修复它,我需要在当前 latLng 的语言环境中替换行 Locale.getDefault()

我试图更改代码并做

geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
geocoder = new Geocoder(this, addresses.get(0).getLocale());
addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);

但它不起作用,因为addresses.get(0).getLocale() 返回电话区域设置而不是地址真实区域设置。

如果你们中的任何一个可以就如何解决我的问题给我任何建议,我很乐意听到。谢谢!

【问题讨论】:

  • 您需要选择点的语言吗?
  • 是的。如果我选择语言,我将能够选择位置名称的语言

标签: android google-maps


【解决方案1】:

我终于找到了解决办法。

我使用了this similar question的答案。 此解决方案与MeosCoder 对此问题的回答非常相似。

现在,我的代码如下所示:

    Geocoder geocoder;
    List<Address> addresses;
    geocoder = new Geocoder(this, Locale.getDefault());
    String markerAddress = "";

    try {
        addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
        String langCode = null;
        String countryCode = null;

        if (addresses.size() > 0) {
            countryCode = addresses.get(0).getCountryCode();
            addresses = null;

            Locale[] locales = Locale.getAvailableLocales();
            for (Locale localeIn : locales) {
                if (countryCode.equalsIgnoreCase(localeIn.getCountry())) {
                    langCode = localeIn.getLanguage();
                    break;
                }
            }

            if (langCode != null) {
                Locale locale = new Locale(langCode, countryCode);
                geocoder = new Geocoder(this, locale);

                try {
                    addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
                } catch (IOException | IndexOutOfBoundsException | NullPointerException ex) {
                    addresses = null;
                }
            }
        }

【讨论】:

    【解决方案2】:

    试试这个。您可以从 latlon 位置获取语言代码或语言名称。

    public String getLanguageCode(double lat, double lon){
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(this, Locale.getDefault());
        String langCode = null;
        try{
            addresses = geocoder.getFromLocation(lat, lon, 1);
            Address address = addresses.get(0);
            String countryCode = address.getCountryCode();
            Locale[] locales = Locale.getAvailableLocales();
    
            for (Locale localeIn : locales) {
                if (countryCode.equalsIgnoreCase(localeIn.getCountry())) {
                    //langCode = localeIn.getLanguage(); //This is language code, ex : en
                    langCode = localeIn.getDisplayLanguage();// This is language name, ex : English
                    break;
                }
            }
        } catch (Exception ex) {
    
        }
    
        return langCode;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      相关资源
      最近更新 更多