Get Latitude and Longitude from Address using PHP and Google Maps API

On 12/12/2017

Hi! In this tutorial, let's see how to get latitude and longitude from address using google maps geocoding api and php. Geocoding is the process of converting physical addresses into geographical coordinates such as latitude and longitude so you can use them to create markers in Google Map. It is also better to provide latitude and longitude when using Google Map API so that you can point to accurate position on the map. To obtain the geocode, we must send an http request to the Google Map Geocoding API along with the formatted address.

php get latitude longitude from address google maps api

How to Get Latitude and Longitude from Address?

The Google Maps Geocoding API is a web service used to obtain geocoding and reverse geocoding of the addresses. And you have to access the api through the HTTP interface.

Here is the sample request you have to send to Geocoding API to get latitude and longitude coordinates.

The API provides a response in both json and xml format. Since json is cross platform compatible, let us inform the api to send the response as json.

// api request
http://maps.google.com/maps/api/geocode/json?address=Brooklyn,+NY,+USA

And the above http request will provide a json response that looks something like this,

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Brooklyn",
               "short_name" : "Brooklyn",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Kings County",
               "short_name" : "Kings County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Brooklyn, NY, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 40.739446,
                  "lng" : -73.83336509999999
               },
               "southwest" : {
                  "lat" : 40.551042,
                  "lng" : -74.05663
               }
            },
            "location" : {
               "lat" : 40.6781784,
               "lng" : -73.94415789999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.739446,
                  "lng" : -73.83336509999999
               },
               "southwest" : {
                  "lat" : 40.551042,
                  "lng" : -74.05663
               }
            }
         },
         "place_id" : "ChIJCSF8lBZEwokRhngABHRcdoI",
         "types" : [ "political", "sublocality", "sublocality_level_1" ]
      }
   ],
   "status" : "OK"
}

Now let's see how to use this geocoding api in php to get latitude and longitude details of an address.

PHP Function to Get Geocoding from Google Maps:

Here is the php function to get geocoding information. The function takes up address of a location, sends an http request to the google map api, decodes the json response and extracts and returns the coordinates of latitude and longitude.

<?php
function getGeoCode($address)
{
        // geocoding api url
        $url = "http://maps.google.com/maps/api/geocode/json?address=$address";
        // send api request
        $geocode = file_get_contents($url);
        $json = json_decode($geocode);
        $data['lat'] = $json->results[0]->geometry->location->lat;
        $data['lng'] = $json->results[0]->geometry->location->lng;
        return $data;
}
?>

Function Usage:

You have to call the getGeoCode() function like below.

<?php
$address = 'Brooklyn, NY, USA';
$address = str_replace(' ', '+', $address);
$result = getGeoCode($address);
echo 'Latitude: ' . $result['lat'] . ', Longitude: ' . $result['lng'];

// produces output
// Latitude: 40.6781784, Longitude: -73.9441579
?>

Please note that the address you specify must be an actual one and should be formatted by replacing the space character with the '+' operator.

Using API Key:

To use the API key, you must log in to your Google account, activate the 'Google Maps Geocoding API' on your account and get an 'API_KEY'.

Then you can include your key using the key parameter on each request like this,

https://maps.googleapis.com/maps/api/geocode/json?address=FORMATTED_ADDRESS&key=YOUR_API_KEY

The earlier versions of the API, includes sensor parameter to determine if your application uses sensor to find the user's location or not. The parameter is deprecated and no longer necessary with the API call.

Read Also:

That explains about getting latitude and longitude coordinates from a physical address in php using Google Maps Geocoding API. Once you get the geocode, you can use them to position the map. I hope you like this tutorial. If you find it useful, please share the post on your social circle.

No comments:

Post a Comment

Contact Form

Name

Email *

Message *