<?php
function getLatLong($address) {
$address = str_replace(' ', '+', $address);
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address='.$address.'&sensor=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$geoloc = curl_exec($ch);
$json = json_decode($geoloc);
return array($json->results[0]->geometry->location->lat, $json->results[0]->geometry->location->lng);
}
$address = getLatLong('Guildford');
$address = getLatLong('BH15 2BT');
$address = getLatLong('10 Downing Street, London');
function Haversine($start, $finish) {
$theta = $start[1] - $finish[1];
$distance = (sin(deg2rad($start[0])) * sin(deg2rad($finish[0]))) + (cos(deg2rad($start[0])) * cos(deg2rad($finish[0])) * cos(deg2rad($theta)));
$distance = acos($distance);
$distance = rad2deg($distance);
$distance = $distance * 60 * 1.1515;
return round($distance, 2);
}
$start = getLatLong('Guildford');
$finish = getLatLong('BH15 2BT');
$distance = Haversine($start, $finish);
print('<p>The distance between ['.$start[0].', '.$start[1].'] and ['.$finish[0].', '.$finish[1].'] is '.$distance.' miles ('.($distance * 1.609344).' km).</p>');
?>
preferences:
41.67 ms | 402 KiB | 5 Q