+

Get the longitude and latitude of an address with Google Maps API

November 15, 2012

The google maps API is amazing, and can be used to create some pretty sweet things. One thing that I have used from it several times is their Geocode API.

Geocoding, according to Google’s Developer documentation is:

Geocoding is the process of converting addresses (like “1600 Amphitheatre Parkway, Mountain View, CA”) into geographic coordinates (like latitude 37.423021 and longitude -122.083739), which you can use to place markers or position the map

This is especially useful if you want to build interactive maps in your websites. One use case I have had is a locations post type in WordPress. This allows a user to enter an address for the location, then I can get the coordinates from the address to place it on a map, use it in a store locator, provide directions, etc.

The API Endpoint is formatted as such:

http://maps.googleapis.com/maps/api/geocode/[FORMAT]?address=[ADDRESS]&sensor=[SENSOR]
FORMAT: is 'json' or 'xml'
SENSOR: 'true' or 'false'
ADDRESS: urlencoded address string.

The below example code takes an address, passes it to the API endpoint and returns a JSON object (if you prefer XML you just have to change the output to xml) from google with the information regarding the point. I use file_get_contents, but you could use curl, wget, or any other functions that would make an HTTP request on the url.


<?php
 $address = urlencode('1060 West Addison Street Chicago, IL 60613'); // Wrigley Field
 $url ='http://maps.googleapis.com/maps/api/geocode/json?address='.$address.'&sensor=false'
 $geocode = file_get_contents($url);
 $results = json_decode($geocode, true);
 if($results['status']=='OK'){
    $lat = $results['results'][0]['geometry']['location']['lat'];
    $long = $results['results'][0]['geometry']['location']['lng'];
 }
?>

Here is the output of the above $result response:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1060",
               "short_name" : "1060",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "W Addison St",
               "short_name" : "W Addison St",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Wrigleyville",
               "short_name" : "Wrigleyville",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Chicago",
               "short_name" : "Chicago",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Chicago",
               "short_name" : "Chicago",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Cook",
               "short_name" : "Cook",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Illinois",
               "short_name" : "IL",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "60657",
               "short_name" : "60657",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1060 W Addison St, Chicago, IL 60657, USA",
         "geometry" : {
            "location" : {
               "lat" : 41.9473040,
               "lng" : -87.6564470
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 41.94865298029149,
                  "lng" : -87.65509801970849
               },
               "southwest" : {
                  "lat" : 41.94595501970849,
                  "lng" : -87.65779598029151
               }
            }
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

What you do with that information is up to you, but now you have the longitude and latitude readily available to you.

Posted in: Tools, Web Development   |   Tags: , , ,

Comments are closed.

« Back to Blog