function distance($lat1, $lon1, $lat2, $lon2, $unit = 'M') { $theta = $lon1 - $lon2; $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $dist = $dist * 60 * 1.1515;
if ($unit == "K") { $dist *= 1.609344; } else if ($unit == "N") { $dist *= 0.8684; }
return round($dist, 1); }
function bearing($lat1, $lon1, $lat2, $lon2) { if (round($lon1, 1) == round($lon2, 1)) { if ($lat1 < $lat2) { $bearing = 0; } else { $bearing = 180; } } else { $dist = distance($lat1, $lon1, $lat2, $lon2, 'N'); $arad = acos((sin(deg2rad($lat2)) - sin(deg2rad($lat1)) * cos(deg2rad($dist / 60))) / (sin(deg2rad($dist / 60)) * cos(deg2rad($lat1)))); $bearing = $arad * 180 / pi(); if (sin(deg2rad($lon2 - $lon1)) < 0) { $bearing = 360 - $bearing; } }
$dirs = array("N","E","S","W");
$rounded = int_round($bearing / 22.5) % 16; if (($rounded % 4) == 0) { $dir = $dirs[$rounded / 4]; } else { $dir = $dirs[2 * floor(((floor($rounded / 4) + 1) % 4) / 2)]; $dir .= $dirs[1 + 2 * floor($rounded / 8)]; #if ($rounded % 2 == 1) # $dir = $dirs[round_to_int($rounded/4) % 4] . "-" . $dir; }
return $dir; #return $bearing; }
|