How to get city name from latitude and longitude coordinates in Google Maps?

UMAR-MOBITSOLUTIONS picture UMAR-MOBITSOLUTIONS · Feb 19, 2010 · Viewed 181.4k times · Source

How might I obtain the city name in Google Maps if I have latitude and longitude coordinates of a town or area?

I tried using the latitude, longitude and I got country but I don't know how to get city name.

Answer

ccheneson picture ccheneson · Feb 19, 2010

From a Geocoder object, you can call the getFromLocation(double, double, int) method. It will return a list of Address objects that have a method getLocality().

Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0) {
    System.out.println(addresses.get(0).getLocality());
}
else {
   // do your stuff
}