12/08/2013

Get the latitude and longitude information from a place name in Android

Android provides Location API for us to get the place name from latitude and longitude information.  However, it does have a means to do the reverse. Here is a solution to do what you solve this issue.

Google provides RESTful API for most its services. Map is one of them. We can use following API to get a response in JSON format:

http://maps.googleapis.com/maps/api/geocode/json?address=CITY_NAME&sensor=false
 JSON-formatted reply:
{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Chiayi County",
               "short_name" : "Chiayi County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Taiwan",
               "short_name" : "TW",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Chiayi County, Taiwan",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 23.6359267,
                  "lng" : 120.9576494
               },
               "southwest" : {
                  "lat" : 23.2147999,
                  "lng" : 120.1174411
               }
            },
            "location" : {
               "lat" : 23.4588948,
               "lng" : 120.5739579
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 23.6359267,
                  "lng" : 120.9576494
               },
               "southwest" : {
                  "lat" : 23.2147999,
                  "lng" : 120.1174411
               }
            }
         },
         "types" : [ "administrative_area_level_2", "political" ]
      }
   ],
   "status" : "OK"
}
Simply parse the reply and we can get the latitude and longitude information.

REF: http://stackoverflow.com/questions/10008108/how-to-get-the-latitude-and-longitude-from-city-name-in-android