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

It’s very simple to get longitude and latitude with the help of Google map API

NOTE: An API (Application Programming Interface) is a set of methods and tools that can be used for building software applications.

View DEMO

http://maps .google.com/maps/api/geocode/json?address=Bangalore&sensor=false

You have seen “Geocode” in above URL.

What is Geocoding?
Geocoding is the process of converting addresses (like “IBM India Pvt Ltd , Vittal Mallya Road, Bengaluru, Karnataka, India”) into geographic coordinates (like latitude 12.9715987 and longitude 77.5945627), which you can use to place markers on a map, or position the map.

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

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Bengaluru",
               "short_name" : "Bengaluru",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Bangalore Urban",
               "short_name" : "Bangalore Urban",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Karnataka",
               "short_name" : "KA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "560001",
               "short_name" : "560001",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Bengaluru, Karnataka 560001, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 13.173706,
                  "lng" : 77.8826809
               },
               "southwest" : {
                  "lat" : 12.7342888,
                  "lng" : 77.3791981
               }
            },
            "location" : {
               "lat" : 12.9715987,
               "lng" : 77.5945627
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 13.173706,
                  "lng" : 77.8826809
               },
               "southwest" : {
                  "lat" : 12.7342888,
                  "lng" : 77.3791981
               }
            }
         },
         "place_id" : "ChIJbU60yXAWrjsR4E9-UejD3_g",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

when I give Bangalore as city name,those JSON object are returned.

But we don’t want to use the URL in browser , we use PHP and return the data
Here I will explain two method for this.

I prefer cURL than file_get_contents because cURL is faster and more stable.

File_get_contents:
Here am passing URL to file_get_contents function, this will return the Json object, pass that to the Json_decode function, then specify the section of the object desired.

$url=file_get_contents("http://maps.google.com/maps/api/geocode/json?address=Bangalore&sensor=false");
$response=json_decode($url);
$lat=$response->results[0]->geometry->location->lat;
$lng=$response->results[0]->geometry->location->lng;

cURL:
To use cURL, first we need to initiate cURL and need to set the option, first option is the url to be called,the second option tells the request to bring data back, the rest specify port and host.
curl_exec actually run the cURL request

See here for more about cURL

$url="http://maps.google.com/maps/api/geocode/json?address=Bangalore&sensor=false";
$ch=curl_init();//initiating cURL
curl_setopt($ch,CURLOPT_URL,$url);// CALLING THE 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);

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

$response=json_decode($response );
$lat=$response->results[0]->geometry->location->lat;
$lng=$response->results[0]->geometry->location->lng;

The best way to pass the address in URL is using urlencode()
This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.

$place="New Delhi";
$place=urlencode($place);
$url="http://maps.google.com/maps/api/geocode/json?address=".$place."&sensor=false";

Here the place name is New Delhi, where there is a white space in it.the white space cant be used in URL.

The function urlencode will make the place convenient to URL.

Complete Code Using cURL


$place="Bangalore";
$place=urlencode($place);
$url="http://maps.google.com/maps/api/geocode/json?address=".$place."&sensor=false";
$ch=curl_init();//initiating curl
curl_setopt($ch,CURLOPT_URL,$url);// CALLING THE 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);

$response=json_decode($response );
$lat=$response->results[0]->geometry->location->lat;
$lng=$response->results[0]->geometry->location->lng;

echo $lat.'  '.$lng;

See Get Address from longitude and latitude with PHP and Google map API