Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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();
}   
like image 711
user1670443 Avatar asked Oct 01 '12 12:10

user1670443


4 Answers

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.

like image 155
Dustfinger Avatar answered Oct 06 '22 17:10

Dustfinger


The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code. This will return the city name and country name

    if (addresses.size() > 0) {
        System.out.println(addresses.get(0).getLocality());
        System.out.println(addresses.get(0).getCountryName());
    }

For details you can look into the Address object

like image 32
Antrromet Avatar answered Oct 06 '22 18:10

Antrromet


To get City, State and Country use code in following way-

if (addresses.size() > 0) {
    System.out.println(addresses.get(0).getLocality());
    System.out.println(addresses.get(0).getAdminArea());
    System.out.println(addresses.get(0).getCountryName());
}
like image 40
Megha Kalmegh Avatar answered Oct 06 '22 18:10

Megha Kalmegh


Have a look at Address (javadoc) which is returned from the geocoder. This has separate methods like getLocality() to return the city org getCountry() to return the country name etc.

like image 20
Heiko Rupp Avatar answered Oct 06 '22 17:10

Heiko Rupp