Question

I have searched quite a bit without luck so far.

The Android Geocoder returns the android.location.Address object. The city, as far as I understood, should be returned in getLocality(). It seems within USA this works well, outside not.

I am writing an international app and struggle to find a solution to find out the city of a geolocation.

Here the output from Czech Republic/Prague :

Address[addressLines=
[0:"Psohlavců 1764/2",
1:"147 00 Prague-Prague 4",
2:"Czech Republic"],
feature=2,
admin=Hlavní město Praha,
sub-admin=Prague,
locality=null,
thoroughfare=Psohlavců,
postalCode=147 00,
countryCode=CZ,
countryName=Czech Republic,
hasLatitude=true,
latitude=50.0276543,
hasLongitude=true,
longitude=14.4183926,
phone=null,
url=null,
extras=null]

locality is null, the city is within sub-admin ! The address itself is ok, so the geocoder server seems to know the city.

Here some ore random EU examples but locality works partly:

Address[addressLines=[0:"Nad lesem 440/34",1:"147 00 Prague-Prague 4",2:"Czech Republic"],feature=34,admin=Hlavní město Praha,sub-admin=Prague,locality=null,thoroughfare=Nad lesem,postalCode=147 00,countryCode=CZ,countryName=Czech Republic,hasLatitude=true,latitude=50.02424,hasLongitude=true,longitude=14.4117568,phone=null,url=null,extras=null]

Address[addressLines=[0:"Hauner Straße 4",1:"84431 Heldenstein",2:"Germany"],feature=4,admin=null,sub-admin=null,locality=Heldenstein,thoroughfare=Hauner Straße,postalCode=84431,countryCode=DE,countryName=Germany,hasLatitude=true,latitude=48.2540274,hasLongitude=true,longitude=12.3413535,phone=null,url=null,extras=null]

Address[addressLines=[0:"Igler Straße",1:"6020 Innsbruck",2:"Austria"],feature=Igler Straße,admin=Tyrol,sub-admin=Innsbruck,locality=Innsbruck,thoroughfare=Igler Straße,postalCode=6020,countryCode=AT,countryName=Austria,hasLatitude=true,latitude=47.2465698,hasLongitude=true,longitude=11.4054237,phone=null,url=null,extras=null]

Address[addressLines=[0:"Durnberg 24",1:"5724 Stuhlfelden",2:"Austria"],feature=24,admin=Salzburg,sub-admin=Zell am See District,locality=null,thoroughfare=Durnberg,postalCode=5724,countryCode=AT,countryName=Austria,hasLatitude=true,latitude=47.3233373,hasLongitude=true,longitude=12.4960482,phone=null,url=null,extras=null]

Address[addressLines=[0:"U Roháčových kasáren 14",1:"100 00 Prague 10",2:"Czech Republic"],feature=14,admin=Hlavní město Praha,sub-admin=Prague,locality=Prague 10,thoroughfare=U Roháčových kasáren,postalCode=null,countryCode=CZ,countryName=Czech Republic,hasLatitude=true,latitude=50.0704092,hasLongitude=true,longitude=14.4673473,phone=null,url=null,extras=null]

Maybe the fault is on me, but to me it seems like depending on the country and area the city will be found in different fields. However, the address itself mostly seems to be good enough to send a postal letter.

Has someone written a clever function which tries to make more sense out of the Geocoder results ? It's a pity to see that Google has the information stored but does not provide it properly.

Was it helpful?

Solution

Going to close my question, solved it with a workaround. Using the suggestion from dannyroa

String city="unknown";
if (address.getLocality() != null)  city=address.getLocality();
else
  if (address.getSubAdminArea() != null)  city=address.getSubAdminArea();

This could be further extended by getting the city information out of the second address line. removing the postal code and taking the remainder, but this information is not unique within a city and could change depending on the district/zone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top