Get longitude and latitude from an address with PHP and Google Maps API

David Carr

Javascript Tutorials PHP & MySQL

Getting longitude and latitude from an address used to be a fairly complex task a few years ago, thankfully its very simple now using Google Maps API!

The way this will work is making a call to the API: 

http://maps.google.com/maps/api/geocode

The above address will make a call to Google, it needs a little more to be complete it needs the format and the address, as well as the region:

http://maps.google.com/maps/api/geocode/json?address=UK+Hull&sensor=false&region=England

The important part to point out there is the address=UK+HUll the address expects an address also since this is a web URL it cannot have spaces instead use + in place of a space.

Going to the URL in a browser would the following JSON object

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Hull",
               "short_name" : "Hull",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "England",
               "short_name" : "England",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United Kingdom",
               "short_name" : "GB",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Hull, UK",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 53.81325020,
                  "lng" : -0.24139640
               },
               "southwest" : {
                  "lat" : 53.71949550,
                  "lng" : -0.42257510
               }
            },
            "location" : {
               "lat" : 53.74567090,
               "lng" : -0.33674130
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 53.81325020,
                  "lng" : -0.24139640
               },
               "southwest" : {
                  "lat" : 53.71949550,
                  "lng" : -0.42257510
               }
            }
         },
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

For this to be useful you don't want to put the URL in the address bar but use PHP to use that address and return the data, there's two ways to go about this either using curl or file_get_contents.

Curl

First up CURL, this is the preferred way it gives much more control, its also faster then file_get_contents.

The following $url variable will be used for both examples

$url = "http://maps.google.com/maps/api/geocode/json?address=UK+Hull&sensor=false&region=England";

Next initiate curl and set the curl options, the fist option is the url to be called the second option tells the request to bring back data, the rest specify the port and hosts.
curl_exec actually runs the curl request.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);

The data from the curl request are returned and stored in $response from there, decode the json object.

$response = json_decode($response);

To specify the item in the object desired go down the result set accordingly, in this case bring back the longitude and latitude and store them to variables.

$lat = $response->results[0]->geometry->location->lat;
$long = $response->results[0]->geometry->location->lng;

Full Curl Code

$url = "http://maps.google.com/maps/api/geocode/json?address=UK+Hull&sensor=false&region=England";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);

$response = json_decode($response);

$lat = $response->results[0]->geometry->location->lat;
$long = $response->results[0]->geometry->location->lng;  

File_get_contents

This time instead of issuing a curl request pass the url to a file_get_contents function this will return the json object, pass that to a json_decode function then specify the section of the object desired. 

$url = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=UK+Hull&sensor=false&region=UK");
$response = json_decode($url);

$lat = $response->results[0]->geometry->location->lat;
$long = $response->results[0]->geometry->location->lng; 

 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Fathom Analytics $10 discount on your first invoice using this link

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.