The below function pulls the Geocode Location Latitude and Longitude values using PHP. It uses the Google API to pull those Geo co-ordinates.
function getLatLogOfAddress($address='')
{
$address = urlencode($address);
//here is the google api url
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false";
//get the content from the api using file_get_contents
$getmap = file_get_contents($url);
//the result is in json format. To decode it use json_decode
$googlemap = json_decode($getmap);
$data = array();
//get the latitute, longitude, address from google api's json result
$googlemap = isset($googlemap->results[0]) ? $googlemap->results[0] : '';
$data['lat'] = isset($googlemap->geometry->location->lat) ? $googlemap->geometry->location->lat : '';
$data['lng'] = isset($googlemap->geometry->location->lng) ? $googlemap->geometry->location->lng : '';
$data['address'] = isset($googlemap->formatted_address) ? $googlemap->formatted_address : $address;
return $data;
}
// Call the function as below;
$address = "Apollo Victoria Theatre, 17 Wilton Road, Westminster, London, SW1V 1LG, UK";
$geocodes = $this->getLatLogOfAddress($address);
print_r($geocodes);
// The Result would be...
Array
(
[lat] => 51.4957746
[lng] => 51.4957746
[address] => London SW1V 1LG, UK
)
No comments:
Post a Comment
Please post any queries and comments here.