Google maps Geocoding API using 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)

Demo

Demo

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

Download

Download

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

 

Know more about Google maps Geocoding API.

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

Posted by:

muni

Related articles

Back to Top