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®ion='.$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
What do you think?