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®ion=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.
Google states that you (normally) are allowed 2500 requests in 24 hours:
https://developers.google.com/maps/documentation/geocoding/#Limits
Hello, How to retrieve GPS data directly to Google maps??
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
That API is limited , we can’t send to much queries
Pls can you tell me what should i do ?
Thank you
Nice piece of a code. But it worth me thousand lines of code. Thanks Elliott.
Thanks Tom for the script … Its working fine.. Thanks
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,
Thank you so much! This short snippet helped me a lot!
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.
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?
Thank you Tom
Afrer get Latitude & Longitude how to find near location
Hi tom,
Thanks. I got it. B
But how do you show location using lat and log in php?
Notice: Undefined offset: 0 in C:\xampp\htdocs\gps\gmap.php on line 8
Notice: Undefined offset: 0 in C:\xampp\htdocs\gps\gmap.php on line 9
latitude: longitude:
I got the error
“Undefined offset: 0
Can you give a suggestion to deal with this!
Hi,
Nice tutorial. Thanks.
Recently am getting OVER_QUERY_LIMIT from this API, Do you have any idea about it?