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
)
*/
Share... Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on TumblrShare on RedditEmail this to someone

2 Comments
Artyom Shmatok
July 20, 2014 at 10:59 am

How to lat, lng convert -> coordinates E N W S … (GPS)?
Thanks.

Dan Kaufman
November 17, 2015 at 6:03 pm

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

Leave a Comment

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

Comment *
Your Name *
Your Email *
Website