PHP get latitude and longitude using google maps API

Example:

$address = "51B Kreta Ayer Street";

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

$json = @file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$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 = 'http://maps.google.com/maps/api/geocode/json?address='.$address.'&sensor=false&region='.$region.' ';
$json = @file_get_contents($query);

reference:
https://developers.google.com/maps/articles/phpsqlsearch_v3#findnearsql
http://stackoverflow.com/questions/8633574/get-latitude-and-longitude-automatically-using-php-api

By Keenlio, April 15, 2014

What do you think?

Leave a Reply

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


− one = 8

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>