Android reverse geocoding getLocality returns often null
Asked Answered
T

4

25

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 locality.

Here is an example:

try {
    Geocoder c = new Geocoder(this, Locale.getDefault());
    double lat = 51.481;
    double lon = 0.0;
    List<Address> l = c.getFromLocation(lat, lon, 5);
    for (Address a: l) {
        Log.i("GeocoderTest", "Locality " + a.getLocality() + " (" + a + ")");
    }
} catch (IOException e) {
    Log.e("GeocoderTest", "", e);
}

This now logs the following message for the first returned address:

Locality null (Address[addressLines=[0:"14-18 Park Vista",1:"London Borough of Greenwich, London SE10",2:"UK"],feature=,admin=null,sub-admin=null,locality=null,thoroughfare=Park Vista,postalCode=null,countryCode=GB,countryName=United Kingdom,hasLatitude=true,latitude=51.4819069,hasLongitude=true,longitude=-6.327E-4,phone=null,url=null,extras=null])

Some locations do return the city in the locality, while a location next to it does not.

So it did work just fine before, actually I had not seen a null locality before. So I guess something must have changed in Google's geocoding service. Any idea what is going on, and is this situation permanent? If yes, what would be the best way to determine the city from a location?

Telling answered 28/12, 2011 at 22:9 Comment(0)
S
12

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<Address> 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;
}
Shaffert answered 14/6, 2016 at 22:5 Comment(4)
Thanks for the snippet. This is better than having null value :)Amadaamadas
Thanks for the update ! @Leo Droidcoder have u ever seen this to fail too ? I mean all the addresses fail ?Complacent
Can fail... It is happens with meBaxie
The code stops at List<Address> addresses = mGeocoder.getFromLocation(lat, lon, 10); for me. What is the problem?Pumice
H
7

Now I live in Canada, Ontario, Hamilton (Hamilton is my city, Ontario is the province)

I noticed that getLocality() returns null, and getAdminArea() returns Ontario, and getSubLocality() returns Hamilton. ch

Hoberthobey answered 17/9, 2012 at 7:29 Comment(0)
A
2

In Kotlin I have done something like this, given the address a

var place = a.locality
if (place == null) place = a.subLocality
if (place == null) place = a.subAdminArea
if (place == null) place = a.adminArea

It works even for remote places

Asquint answered 29/5, 2021 at 10:33 Comment(1)
Instead you can also use a.locality ?: a.subLocality ?: a.subAdminArea ?: a.adminAreaCantabrigian
F
0

Using getSubAdminArea() worked fine for me.

Foote answered 11/6, 2020 at 23:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.