Get latitude,longitude from address in Android

Christos Baziotis picture Christos Baziotis · Jul 24, 2013 · Viewed 37.2k times · Source

I want to get the latitude and longitude of a place from an address. I don't want to get the latitude and longitude from the GPS nor the the network.

I want the user to be able to type in a TextView the address of the place he wants (ex. his office) and below the textfield some suggestions will be shown, just like when you type the address in the google maps app. Then i want to retrieve the coordinates of the given address.

If this can't be done is there a way to get the address via the google maps app itself. Perhaps i could call the gmaps from my app and the user will type the address and the gmaps would return the coordinates.

How can i do it?

Answer

Demitrian picture Demitrian · Jul 24, 2013

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.