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

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.

24 thoughts on “How to get Latitude/Longitude from an address (or Geocoding ) using PHP

  1. 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. 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!

  3. 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.

  4. 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.

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

  6. 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.

  7. $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;

  8. 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:apachefriendsxampphtdocsstudentPortaltest.php on line 4 please tell me what is wrong.

  9. 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!

  10. 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.

  11. Pingback: Tech Tips | Calculating the Distance between Coordinates

  12. When i run this code on localhost there is no problem but when i put in online i’ve got the status: query_limit. in the google documentation i read that it only works with a google maps… someone nows more about it?

  13. Hi,

    I tried this code to get latitude and longitude by address but it returns null in print_r($geocode).

    $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’);

    When i run url direct in browser its return array with values.
    In my server allow_url_fopen is set to ON.

    Please give some suggestion what to do?

    Thanks

    • i think your code is divided into two lines in editor , Please make sure the whole URL appear in one line in your code.

      Thanks

  14. Pingback: Como recuperar a Latitude/Longitude (Geocode) de um endereço com PHP | Beto

  15. Hello Everyone, i am using above code and is working fine without app key but when i put my app key in URL , it does not return latLong . Please somebody help me with this.

    Thanks

Comments are closed.