8

Below is my code, I am getting null subLocality always ....

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses  = geocoder.getFromLocation(latitude,longitude, 1);

String suburb = addresses.get(0).getSubLocality();
String state = addresses.get(0).getAdminArea();
String zip = addresses.get(0).getPostalCode();
String country = addresses.get(0).getCountryName();

Log.e("Log for data",""+suburb+" "+state+" "+zip+" "+country);

Below is my response;

null Gujarat 380006 India
Zoe
  • 27,060
  • 21
  • 118
  • 148
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
  • Possible duplicate of [Android reverse geocoding getLocality returns often null](https://stackoverflow.com/questions/8661857/android-reverse-geocoding-getlocality-returns-often-null) – theapache64 May 08 '19 at 11:14
  • Yes, Geocoder makes issue sometimes *(I faced it in past, for some OEM it was always null)*. As an alternative solution, I migrated to **following API** as solution for *Geo-Coding* : https://developers.google.com/maps/documentation/geocoding/start#reverse. – Jeel Vankhede May 13 '19 at 06:20
  • Hello @JeelVankhede ... How can I find subloaclity name from this webservice. In this big response which parameter I have to take? – Vishal Vaishnav May 13 '19 at 09:11
  • You can determine it from `"types"` in that response if you've observed it closely. – Jeel Vankhede May 13 '19 at 09:32

2 Answers2

4

Geocoder API will return the values depending on lat long, if the API database doesn't have these values exemple : subLocality then it will return null only, in my knowledge there's nothing you can do about it. You have to handle these cases gracefully so your app don't crash.

Handling these cases includes you have to put a check using if condition is that particular value is null or it has some value, because if you use any of the value somewhere in code without checking and if that value is null then app might crash whenever that particular value comes null.

ismail alaoui
  • 5,748
  • 2
  • 21
  • 38
0

I too had the same problem which was solved by increasing the number of results which you request from Geocoder API.

Try increasing it to 10

List<Address> addresses  = geocoder.getFromLocation(latitude,longitude, 10);

Then if your sub-locality is null with the first result, try search in the rest of them for the same

Veeresh Charantimath
  • 4,641
  • 5
  • 27
  • 36