android - Latitude and longitude addresses for Google Maps always show Chinese locations -
the problem
i'm trying retrieve latitude , longitude address using geocoder.getfromlocationname().
it works android devices, keeps showing chinese latitude , longitude addresses xiamoi mobile phone. however, should showing location - saket, delhi, india.
here's code:
public latlng getlocationfromaddress(string straddress) { geocoder coder = new geocoder(getactivity(), locale.getdefault()); list<address> address; latlng p1 = null; try { address = coder.getfromlocationname(straddress, 5); if (address == null) { return null; } address location = address.get(0); location.getlatitude(); location.getlongitude(); p1 = new latlng(location.getlatitude(), location.getlongitude()); return p1; } }
change locale
the locale passed geocoder constructor represents desired locale query results. applies the following code:
geocoder coder = new geocoder(getactivity(), locale.getdefault());
be wary of default locale
from documentation locale.getdefault();
note there many convenience methods automatically use default locale, using them may lead subtle bugs.
a common mistake implicitly use default locale when producing output meant machine-readable. tends work on developer's test devices (especially because many developers use en_us), fails when run on device user in more complex locale.
the default locale china. makes sense seeing xiaomi chinese manufacturer. why keep getting chinese addresses.
test
try following , check logs see locale being set to:
locale defaultlocale = locale.getdefault(); log.i("myactivity", defaultlocale.tostring());
Comments
Post a Comment