PHP get latitude and longitude using google maps API


$address = "51B Kreta Ayer Street";

$address = str_replace(" ", "+", $address);

$json = @file_get_contents("$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

Output longitude and latitude which can be very useful for location services.

Detailed variable setting see below

//for city
$region = 'city';
//for country
$region = 'country';
//address only
$address = 'address';
//address and zip code
$address = 'address' . 'zip'
//urlencode the variables
$region = urlencode($region);
$address = urlencode($address);

$query = ''.$address.'&sensor=false&region='.$region.' ';
$json = @file_get_contents($query);


By Keenlio, April 15, 2014

What do you think?

Leave a Reply

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

− two = 6

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>