How to get GPS coordinates from an address with PHP and Google Maps
With this simple method in PHP we can get the GPS coordinates from an address using Google Maps.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php function getCoordinates($address){ $address = urlencode($address); $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=" . $address; $response = file_get_contents($url); $json = json_decode($response,true); $lat = $json['results'][0]['geometry']['location']['lat']; $lng = $json['results'][0]['geometry']['location']['lng']; return array($lat, $lng); } $coords = getCoordinates("Wall Street, New York"); print_r($coords); /* Prints: Array ( [0] => 41.3936254 [1] => 2.1634189 ) */ |
2 Comments
How to lat, lng convert -> coordinates E N W S … (GPS)?
Thanks.
Freaking awesome! thank you so much. I’ve been fighting with a JS version of this for hours now. The result is going to PHP for MySQL storage anyway so this did the trick. Simple, elegant and functional.
THANKS!!!