With the geocoder API, I am trying to get the complete address of the user and need to specify the city, state, and country separately. However, I am unable to get the city parameter correctly. It sometimes picks an area name and leaves the rest null.
Any workarounds that I can use? I don't have access to paid APIs, so please suggest something that is free.
Geocoder gcd = new Geocoder(Context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(
lastLocation.getLatitude(), lastLocation.getLongitude(), 1);
if(addresses.get(0).getCountryName() !=null)
country =addresses.get(0).getCountryName();
if(addresses.get(0).getAdminArea() !=null)
state =addresses.get(0).getAdminArea();
if(addresses.get(0).getSubAdminArea() !=null)
{
city = addresses.get(0).getSubAdminArea();
} else if(addresses.get(0).getLocality() !=null)
{
city = addresses.get(0).getLocality();
}
Although I checked all the parameters separately, I was not able to locate a specific parameter that returns the correct city name every time. I have already checked other posts, but have not found any reliable ones.