I am using Android Geocoding to get the current city with Address.getLocality()
. It has worked fine, until just recently it appears to often return null for the loc
I noticed, that very often getLocality() returns null for the first address in the list, returned by Geocoder.
On the other hand correct city name stays at Locality of a next Address.
So I am using this workaround and it works well for big cities:
private String getCityNameByCoordinates(double lat, double lon) throws IOException {
List addresses = mGeocoder.getFromLocation(lat, lon, 10);
if (addresses != null && addresses.size() > 0) {
for (Address adr : addresses) {
if (adr.getLocality() != null && adr.getLocality().length() > 0) {
return adr.getLocality();
}
}
}
return null;
}