How to get Latitude/Longitude from an address (or Geocoding ) using PHP

March 10th, 2010 | Save to del.icio.us now(0)

While Google’s documents for maps API does good job in showing how to get lat/long from an address in JavaScript they do not really show any example of doing the same with PHP.

 

So to make you life simple here is a small script in PHP that does that.

 

 

$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address=573/1,+Jangli+Maharaj+Road,+Deccan+Gymkhana,+Pune,+Maharashtra,+India&sensor=false');

$output= json_decode($geocode);

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

 

The line above makes a request to Google maps API. Passes the address, and receives the response in JSON format.

The URL has following options

http://maps.google.com/maps/api/geocode/output?parameters

where output can be 1) JSON or 2) XML

For more details about parameters check out the Google’s geocoding documentation.

Related posts

  1. Configuring wireless routers
  2. How To Catch PHP Fatal Error In CodeIgniter
  3. Integrating With Twitter API In CodeIgniter
  4. Where is MySQL Gone Away?
  5. Installing PEAR and PHPUnit on WAMP and Windows 7

Wondering what to do next?


(Search web development related contents)


16 Responses to “How to get Latitude/Longitude from an address (or Geocoding ) using PHP”

  1. May 5, 2010 10:15 am Harish shilamkar says

    fanatastic work my friend…. its very usefull blog for all web developers specially PHP developers..u said blog is under construction… is any help required? i know little wordpress..

  2. August 24, 2010 8:02 pm elali says

    Thanx so much, this was extremely helpful

  3. August 25, 2010 10:36 am sameer says

    Hi,

    Thanks you very much

    Have dream day.

  4. August 25, 2010 9:57 pm Dan says

    I have been searching and searching trying to find a simple geocoder means for taking a city and state address and geocoding it for the latitude and longitude in PHP. I was about to give up and use the javascript, but then landed to your wonderful page.

    Thank you!

  5. August 27, 2010 7:16 pm Vipul says

    Hi,

    I put this code in one of my php file but its give me “no result”.

    Any additional js requires to put.Help is appreciate.

  6. September 8, 2010 4:00 am AL says

    Great piece of codes.

    But when I tried to pass the address with special characters, for example German characters, it will not return any result. It returned as “Invalid Request”. Anyone knows how to pass special characters in address?

    Thanks.

  7. February 2, 2011 2:17 pm Dhanesh says

    How can i identify a specific location is included in a kml file of google earth?.Is any code for that using php

  8. March 25, 2011 7:40 pm Mike says

    Thank you so much for this code. After spending so long I managed to find your site and your example works within seconds! Amazing and thanks again.

  9. April 1, 2011 11:30 pm BobS says

    Unfortunately – it gives incorrect coordinates…
    I was hoping that it would be accurate!

  10. April 12, 2011 1:39 pm Vaishakh says

    Great work dude , however Google Maps has a request rate of only 2000 queries a day . I have written a similar code that gives me a geocoding
    rate of 50,000 queries a day . You can get the code at
    http://googlelatiude.blogspot.com/2011/04/geocode-address.html

  11. April 17, 2011 12:02 pm Vinay says

    Thanks @vaishakh your blog was really helpful .

  12. May 11, 2011 8:37 pm Vaishakh says
  13. June 15, 2011 7:05 pm nmnm says

    $geocode=file_get_contents(‘http://maps.google.com/maps/api/geocode/json?address=573/1,+Jangli+Maharaj+Road,+Deccan+Gymkhana,+Pune,+Maharashtra,+India&sensor=false‘);

    $output= json_decode($geocode);

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

  14. August 15, 2011 12:37 am Johnson Obaro Isreal says

    I paste The Code Into my PHP file How to Get Your Location Lat and Long . But It Did work It Shows Error= Fatal error: Call to undefined function json_decode() in C:\apachefriends\xampp\htdocs\studentPortal\test.php on line 4 please tell me what is wrong.

  15. September 7, 2011 7:26 pm sigristr says

    Thanks. this is a very neat bit of code, though I had problems which led me to exploring parsing json – ruthlessly exploiting my php error log. After changing the file to a local one (generated by calling the google site from a browser with the request) to prove the principle, I discovered that, in my case, the single quotes round the URL had to be double. The hour was not wasted, as I learnt a lot abour json!

  16. September 19, 2011 3:10 pm rock says

    how can we show the location on google map when visitor click on the address(address is coming from DB).when visitor click on the address,location should be shown on google map.Help is appreciated.

Trackback URI | Comments RSS

Say Something, and Be Counted

Name (required)

Email (required)

Website

Speak your mind