Get longitude & latitude values with Google Maps API using PHP

19 November, 2013 by Tom Elliott

Here’s a handy few lines of PHP code for anyone wanting to get the latitude and longitude values from Google Maps API, based on the town, city or country location.

It took me a little while to get this to work. For some reason the various cURL and file_get_contents methods I saw online to get longitude and latitude values didn’t work for me and I needed to add ‘true’ to the ‘file_get_contents’ command for json_decode to work correctly. Hopefully this will help someone else out 🙂

<?php
$url = "http://maps.google.com/maps/api/geocode/json?address=West+Bridgford&sensor=false&region=UK";
$response = file_get_contents($url);
$response = json_decode($response, true);

//print_r($response);

$lat = $response['results'][0]['geometry']['location']['lat'];
$long = $response['results'][0]['geometry']['location']['lng'];

echo "latitude: " . $lat . " longitude: " . $long;
?>

The http://maps.google.com/maps/api/geocode/json URL takes in 3 parameters: address (your main location), region (optional but can help with ambiguity) and sensor (indicates whether or not the request will come from a device with a locational sensor).

The print_r command commented out above is a useful way to work out the array structure that you need to drill down to. To get the longitude and latitude values, we need to drill down through 5 levels.



20 Comments

  • Hariprasad says:

    Hi,

    Nice tutorial. Thanks.

    Recently am getting OVER_QUERY_LIMIT from this API, Do you have any idea about it?

  • Satish says:

    Hello, How to retrieve GPS data directly to Google maps??

  • Jeremy Green says:

    Thanks for posting this snippet! Helped a ton on my project.

  • Hey Tom! We’re working with an outdoor shopping center, so all the Retailers share a lot of the same address an have a Unit # for mail purposes, however, Google doesn’t really pick up these accurately.

    We coded in a Lat/Long for when we add a Retailer to our WP site and the coordinates are way off. Even when we go to Google Maps and just click on their exact spot in the shopping center and retrieve the Lat/Long numbers. They still are off or show up across the street or in the woods or on another store’s location.

    We found this concept for a great map here [removed] which you can see is very accurate, however, we can’t get ours to function like this. Any tips or something you see this example site is doing which we could implement? – Many thanks, Patrick

    • Tom Elliott says:

      Hi Patrick, yeah the method in the post may only be accurate to the radius for the recognised searched location, or within a central postcode area which may not be very accurate. A method I’ve found that works to get exact lat and long is to find the location you want on a Google Map, zoom right in using satellite view, shrink the browser window to center the marker in a small area and then copy and paste the last latitude and longitude values that google give you in the share link.

      Hope that helps! 🙂

      • Thanks, Tom! I know I’m SUPER late to reply, but in taking what you offered as an alternate method to fine tune the Lat/Long, we created some pretty cool maps for our shopping center clients. The only time they are a bit off the mark is when you begin to rotate in Google Maps, but overall it is spot on.

        Much appreciated! – Patrick

  • Soufiane says:

    That API is limited , we can’t send to much queries
    Pls can you tell me what should i do ?
    Thank you

    • Tom Elliott says:

      Hi Soufiane, if you’re running out of API requests with the PHP method then you may be better off with a JavaScript solution. One problem with doing this server-side is that an API request will also be triggered by search engine crawlers. For a single map on a contact page for example, this shouldn’t be an issue but could be for many dynamically generated maps. Other solutions could either be to reduce the crawl frequency on the site, cache the requests or upgrade to a license that will allow for more requests 🙂

  • anil says:

    Nice piece of a code. But it worth me thousand lines of code. Thanks Elliott.

  • Vishnu says:

    Thanks Tom for the script … Its working fine.. Thanks

  • Tim says:

    Nice code and the instructions to find the exact latitude and longitude of a location work well. I have the converse problem — how to get the exact location on a map from a provided latitude and longitude. The Android app Where’s My Droid will provide the lat and lon of a missing Droid. But when I enter the values it provides in the Google maps search bar, the map provides a location about 9 miles away with a different lat and lon. By using your method above to get the lat and lon of locations I can eventually get the location I want. But how can I get Google maps to just show me the location I want. These are the numbers I used: WMD provided a location at 34.14189, -118.140263 (which is a few hundred feet from my home in Pasadena, CA). A search for these coordinates in the Google search bar gets a red marker in Dodger Stadium about 9 miles away and with coordinates, provided by Google maps of 34deg 04′ 14.2″N 118deg 14′ 26.4″ W (which converts to approx 34.07061, -118.240667). How do I get the correct location from the given lat and lon?
    Thanks,

  • Natalia says:

    Thank you so much! This short snippet helped me a lot!

  • Atul Baraiya says:

    I am getting this error.

    tdClass Object
    (
    [error_message] => You have exceeded your daily request quota for this API.
    [results] => Array
    (
    )

    [status] => OVER_QUERY_LIMIT
    )
    Please tell me how can I handle this.

  • Anthony Verrier says:

    Hello Tom, thanks for this script. Looks great on my website and Safari but there is a problem and that is that the map does not show in Chrome or Firefox.. any ideas how this can be fixed?

  • Vipin says:

    Afrer get Latitude & Longitude how to find near location

  • Adli says:

    Hi tom,
    Thanks. I got it. B

    But how do you show location using lat and log in php?

  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    css.php