Getting sub locality null for some lat long?
Asked Answered
P

2

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
Piacular answered 8/5, 2019 at 11:4 Comment(4)
Possible duplicate of Android reverse geocoding getLocality returns often nullEcklund
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 : developers.google.com/maps/documentation/geocoding/….Lazurite
Hello @JeelVankhede ... How can I find subloaclity name from this webservice. In this big response which parameter I have to take?Piacular
You can determine it from "types" in that response if you've observed it closely.Lazurite
T
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.

Threemaster answered 16/5, 2019 at 17:41 Comment(0)
E
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

Exhilaration answered 12/8, 2019 at 5:12 Comment(2)
This can increase very memory usage or not? In the level of this can to cause lag on my application.Thrown
By example, previously i was using 1 max result and i up to 10, it is can that's to cause delay?Thrown

© 2022 - 2024 — McMap. All rights reserved.