Skip to content Skip to sidebar Skip to footer

How To Get Geocoder’s Results On The Latlng’s Country Language?

I use reverse geocoding in my app to transform LatLng objects to string addresses. I have to get its results not on device’s default language, but on the language of the country

Solution 1:

In your code, Geocoder returns address text in device locale(language).

1 From first element of "addresses" list, get Country Code.

Addressaddress= addresses.get(0);
    StringcountryCode= address.getCountryCode

Then returns Country Code (e.g. "MX")

2 Get Country Name.

StringlangCode=null;

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

3 Instantiate Locale and Geocoder again, and request again.

Localelocale=newLocale(langCode, countryCode);
    geocoder = newGeocoder(this, locale);

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

This worked for me, hopefully for you too!

Post a Comment for "How To Get Geocoder’s Results On The Latlng’s Country Language?"