Google maps Geocoding API using PHP

Posted by & filed under Google, PHP.

Google maps Geocoding API :

Geocoding is the process of converting the given human readable address (no:32, New Street, T nagar,Chennai, India) into geographical co-ordinates (latitude : 15.467 , longitude: 45.789).

no:32, new street, t nagar, chennai, India     ——————– latitude (15.467) & longitude (45.789)

(human readable address)                                                               (Geographical co-ordinates)

 

 

Google Maps geocode API using PHP

Google Maps geocode API using PHP

 

So Google Maps Geo-code API is  a web service, which gives JSON or XML formatted output when you send input in right format  Here is URL of the Google Maps Geo-code API web service.

Example URL :

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false

Working URL :

http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=true

Where

$address (no:32, new street, t nagar, chennai, india) — is PHP variable which contains user entered address

(note : sensor parameter neither true or false in the URL while requesting result)

here is output of above given address :

{ “results” : [ { “address_components” : [ { “long_name” : “New Street”, “short_name” : “New Street”, “types” : [ “route” ] }, { “long_name” : “West Mambalam”, “short_name” : “West Mambalam”, “types” : [ “sublocality”, “political” ] }, { “long_name” : “Chennai”, “short_name” : “Chennai”, “types” : [ “locality”, “political” ] }, { “long_name” : “Chennai”, “short_name” : “Chennai”, “types” : [ “administrative_area_level_2”, “political” ] }, { “long_name” : “Tamil Nadu”, “short_name” : “TN”, “types” : [ “administrative_area_level_1”, “political” ] }, { “long_name” : “India”, “short_name” : “IN”, “types” : [ “country”, “political” ] } ], “formatted_address” : “New Street, West Mambalam, Chennai, Tamil Nadu, India”, “geometry” : { “bounds” : { “northeast” : { “lat” : 13.02902530, “lng” : 80.22394439999999 }, “southwest” : { “lat” : 13.02894270, “lng” : 80.22178819999999 } }, “location” : { “lat” : 13.02896780, “lng” : 80.22287480 }, “location_type” : “GEOMETRIC_CENTER”, “viewport” : { “northeast” : { “lat” : 13.03033298029150, “lng” : 80.22421528029150 }, “southwest” : { “lat” : 13.02763501970850, “lng” : 80.22151731970850 } } }, “partial_match” : true, “types” : [ “route” ] } ], “status” : “OK” }

Now we are going get latitude and longitude co-ordinates of given address using PHP.

Steps:

1. Getting user entered address, then encoding that address using urlencode() method.

2. Sending that url request with encoded address to that Google Maps Geocode API.

3. Google Maps Geocode API provides JSON formatted string output, now converting that JSON formatted string into JSON object using json_decode() method.

4. Finally getting latitude and longitude co-ordinates from the JSON output.

Here is the script to download

 

 

Here php script to find out geo-ordinates of given address using Google Maps Geocode API

 

 <?php
 if(isset($_POST['address'])){
    if($_POST['address']!=null && $_POST['address']!=""){
	 $address=$_POST['address'];
	 $whereurl = urlencode($address);
	 $location = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=$whereurl&sensor=true");
	 //print_r($location);
	 $a=json_decode($location);
	 }else{
          echo "please enter your address to get Geographical co-ordinates";
        }
}
 ?>

<p><b><?php echo "Latitude : ". $a->results[0]->geometry->location->lat;?></b></p>
 <p><b>
 <?php echo "Longitude : ". $a->results[0]->geometry->location->lng;?></b></p>

Know more about Google maps Geocoding API.

Please leave your feedback in the comment box, if you came across any difficulty..

Download Premium Only Scripts & 80+ Demo scripts Instantly at just 1.95 USD per month + 10% discount to all Exclusive Scripts

If you want any of my script need to be customized according to your business requirement,

Please feel free to contact me [at] muni2explore[at]gmail.com

Note: But it will be charged based on your customization requirement

Get Updates, Scripts & Other Useful Resources to your Email

Join 10,000+ Happy Subscribers on feedburner. Click to Subscribe (We don't send spam)
Every Email Subsciber could have access to download 100+ demo scripts & all future scripts.

Get Instant Script Download Access!