简体   繁体   中英

Get location name in the language of its country

I am developing an android app that shows parks around the world and I got stack with the next problem:

In my app I want the user to pick a place on the map that the new park that he is creating will be at this point, and I want other people to be able to see his park no matter from where they are. So in order to do so I need to make sure that the park's location name is in the language of the park's country. For example if the park is in Greece, its location name will be in Greek. If the park is on USA, its name will be in English. So in order to make it possible, I need to be able to get the Locale of the Address of the park.

In order to get the park's name I use the following code:

    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;
    }

The problem is that this code returns the location in the phone default language. In order to fix it I need to replace the line Locale.getDefault() in the Locale of the current latLng.

I tried to change the code and do

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);

But it didn't work because addresses.get(0).getLocale() returns the phone locale and not the address real Locale.

If one of you can give me any suggestion on how to fix my problem I will love to hear it. Thank you!

Eventually I have found the solution.

I have used the answer of this similar question . This solution is very similar with MeosCoder 's answer to this question.

Now, my code looks like this:

    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;
                }
            }
        }

Try this. You can get language code or language name from latlon location.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM