Sachin Soma
Sachin Soma

Reputation: 3802

GeoCoder is returning one of value as null

I am trying to get details of Country,State,City,Pincode,SubRegion,Address.But sometimes it returns City as null or SubRegion as null.Heres the image attachedenter image description here

Here's my code..

 String locationCountry = "", locationState = "", locationCity = "", locationPincode = "", locationSubRegion = "", locationAddress = "";
    Geocoder gCoder;
    List<Address> addresses;
    double latitude = 0, longitude = 0;


    GPStracker g = new GPStracker(getApplicationContext());
            Location l = g.getLocation();


            if (l != null) {
                latitude = l.getLatitude();
                longitude = l.getLongitude();

            }else {
                 l = g.getLocation();
            }

            gCoder = new Geocoder(this, Locale.getDefault());

            try {
                addresses = gCoder.getFromLocation(latitude, longitude, 1);
                if (addresses != null && addresses.size() > 0) {

                    locationCountry = addresses.get(0).getCountryName();
                    locationState = addresses.get(0).getAdminArea();
                    locationCity = addresses.get(0).getLocality();
                    locationPincode = addresses.get(0).getPostalCode();
                    locationSubRegion = addresses.get(0).getSubLocality();
                    locationAddress = addresses.get(0).getAddressLine(0);

                }

            } catch (Exception e) {
            }

Upvotes: 0

Views: 762

Answers (1)

Kiran Malvi
Kiran Malvi

Reputation: 636

Geo coder API will return the values depending on lat long, if the API database doesn't have these values(city 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.

Upvotes: 1

Related Questions