Android : Setting language depending on the current location/country from geocoder

on Saturday, October 25, 2014


I have a textview in my app which shows the current city and street from geocoder and want the city and streetname in the language of the country the user is currently in.


I wanted to do this in the first place (simplified code):


1. Get addresses-list from geocoder:



List<Address> addresses = geocoder.getFromLocation(mLatitude, mLongitude(), 1);


2. Get locale from addresses (docs say it gives me the locale of the current address):



Locale locale = addresses.get(0).getLocale();


3. Create new geocoder-object with this locale:



Geocoder geocoder_new = new Geocoder(mContext, locale);


4. Get addresses from geocoder_new for getting finally city and street names


.....unfortunately getLocale() doesnt really return the locale of the current address but the defaultLocale of my app. Also discussed here:


http://code.google.com/p/android/issues/detail?id=21282


So this doesnt give me what i want.


I did it like this then:



List<Address> addresses = geocoder.getFromLocation(mLatitude, mLongitude, 1);
Locale locale=new Locale(addresses.get(0).getCountryCode());
.......
Geocoder geocoder_new = new Geocoder(mContext, locale);


it works here in germany, but i am not sure if thats gonna work always because docs say:



public String getCountryCode ():
Returns the country code of the address, for example "US", or null if it is unknown.


and



Locale(String language):
Constructs a new Locale using the specified language.


Locale constructor expects a language-string as parameter. Does the countryCode from addresses always have the right format?


Should i do it different?


thx in advance!


0 comments:

Post a Comment