How to get only City, State, Country from lat, long in android

user1670443 picture user1670443 · Oct 1, 2012 · Viewed 43.2k times · Source

I have the below code to convert lat, long to human readable address. Now iam getting full details including street name. How can i get only city, state, country? I don't want anything more details. Please help me.

Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try {
   List<Address> addresses = geoCoder.getFromLocation(latitude, longitude, 1);

   String add = "";
   if (addresses.size() > 0) 
   {
      for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
     add += addresses.get(0).getAddressLine(i) + "\n";
   }

   showToastMessage(add);
}
catch (IOException e1) {                
   e1.printStackTrace();
}   

Answer

Dustfinger picture Dustfinger · Nov 27, 2013

Use the following methods on the objects from addresses:

getLocality()

getCountryName()

Use

getAdminArea() to return state.

I do not know how accurate this is for other countries but for addresses in the USA, this returns the correct state name.

P.S. Sorry for writing a whole separate answer, I am not able to make comments yet.