大量借鉴这里的技术并使用这里的数据,我把这个例子放在一起
<?php
$chicago = array(
'lat' => 41.9
, 'lng' => 87.65
);
$dallas = array(
'lat' => 32.73
, 'lng' => 96.97
);
$ftworth = array(
'lat' => 32.82
, 'lng' => 97.35
);
$bearing = getBearingBetweenPoints( $dallas, $chicago );
echo "Bearing: $bearing°<br>";
echo "Direction: " . getCompassDirection( $bearing );
function getBearingBetweenPoints( $point1, $point2 )
{
return getRhumbLineBearing( $point1['lat'], $point2['lng'], $point2['lat'], $point1['lng'] );
}
function getRhumbLineBearing($lat1, $lon1, $lat2, $lon2) {
//difference in longitudinal coordinates
$dLon = deg2rad($lon2) - deg2rad($lon1);
//difference in the phi of latitudinal coordinates
$dPhi = log(tan(deg2rad($lat2) / 2 + pi() / 4) / tan(deg2rad($lat1) / 2 + pi() / 4));
//we need to recalculate $dLon if it is greater than pi
if(abs($dLon) > pi()) {
if($dLon > 0) {
$dLon = (2 * pi() - $dLon) * -1;
}
else {
$dLon = 2 * pi() + $dLon;
}
}
//return the angle, normalized
return (rad2deg(atan2($dLon, $dPhi)) + 360) % 360;
}
function getCompassDirection( $bearing )
{
static $cardinals = array( 'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'N' );
return $cardinals[round( $bearing / 45 )];
}