Search code examples
androidgoogle-geocoderstreet-address

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


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();
}   

Solution

  • 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