Get latitude,longitude from address in Android

The Geocoder API provided in the Android Framework

I have previously worked with the Geocoding API which exists in the Android API but it does not work on all devices. In fact, as per my and others experiences, a lot of devices will return null when using the Geocoding API.

For that reason, I have chosen to use the Reverse Geocoder which works perfectly on all devices, but requires an additional overhead due to the HTTP request.

Retrieving the longitude and latitude from an address using the Reverse Geocoding API

To avoid this issue, it is a simple matter of using the Reverse Geocoding API which returns a JSON Object.

You can either use the API to find an address through latitude and longitude, or find latitude and longitude from an address:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false

Returns:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara",
               "short_name" : "Santa Clara",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.42291810,
               "lng" : -122.08542120
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.42426708029149,
                  "lng" : -122.0840722197085
               },
               "southwest" : {
                  "lat" : 37.42156911970850,
                  "lng" : -122.0867701802915
               }
            }
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

It is hereby a simple matter of sending a HTTP request to the Reverse Geocoding API with an address entered by the user, and thereafter parsing the JSON Object to find the data you need.

A previous post describes how a JSON object can be parsed from an URL.

Hope this helps.

Leave a Comment