我有一个从位置(纬度,经度)到位置(纬度,经度)。在计算之后,它应该告诉我使用指南针最接近的方法是什么。下面是PHP代码来实现这一点,但是它显示了错误的方向,在这方面我不需要什么帮助。
function GreatCircleDirection ($OrigLat, $DestLat, $OrigLong, $DestLong, $Distance)
{
$Result = 0.0;
$L1 = deg2rad($OrigLat);
$L2 = deg2rad($DestLat);
$D = deg2rad($Distance / 60); # divide by 60 for nautical miles NM to degree
$I1 = deg2rad($OrigLong);
$I2 = deg2rad($DestLong);
$Dlong = $I1 - $I2;
$A = sin($L2) - cos($D + $L1 - pi() / 2);
$B = acos($A / (cos($L1) * sin($D)) + 1);
if ((abs($Dlong) < pi() and $Dlong < 0) or (abs($Dlong) > pi() and $Dlong > 0))
{
//$B = (2 * pi()) - $B;
}
$Result = $B;
return rad2deg($Result);
}
function GreatCircleDistance ($OrigLat , $DestLat, $OrigLong, $DestLong)
{
$L1 = deg2rad($OrigLat);
$L2 = deg2rad($DestLat);
$I1 = deg2rad($OrigLong);
$I2 = deg2rad($DestLong);
$D = acos(cos($L1 - $L2) - (1 - cos($I1 - $I2)) * cos($L1) * cos($L2));
# One degree of such an arc on the earth's surface is 60 international nautical miles NM
return rad2deg($D * 60);
}Bug on if条件:--这是greatCircleDirection函数的if条件中的值,需要知道要修改什么来修复它。
if (0.57700585070933 < 3.1415926535898 and 0.57700585070933 < 0) or (0.57700585070933 > 3.1415926535898 and 0.57700585070933 > 0)示例:
from lat: 33.71,
to lat: 21,
from long: 73.06,
to long: 40 ,
distance: 1908.842544944
direction 104.96527938779 (direction should be 255.87 or so)发布于 2010-02-21 05:49:27
计算距离是不必要的;它只是增加了更多的操作,并可能带来更多的数值错误。使用您的编码风格,类似这样的东西应该可以工作:
function GreatCircleDirection($OrigLat, $OrigLong, $DestLat, $DestLong)
{
$L1 = deg2rad($OrigLat);
$I1 = deg2rad($OrigLong);
$L2 = deg2rad($DestLat);
$I2 = deg2rad($DestLong);
return rad2deg(atan2((sin($I2-$I1),cos($L1)*tan($L2)-sin($L1)*cos($I2-$I1)));
}atan2函数负责识别方向的正确象限,并给出从正北测量到的-180到180之间的角度,例如,GreaterCircleDirection(39,-77,21,40)的计算值为56.76度。符号惯例:北纬为正,南为负;东经为正,西为负。
这一计算在http://patriot.net/~abdali/ftp/qibla.pdf等地进行了讨论。
发布于 2010-02-20 11:34:11
你的距离计算结果出来了。但是我看到,对于初始轴承的答案是(0+105)mod360,而不是(0-105)mod360 (大约),所以我怀疑GreatCircleDirection函数中的if语句中有错误的符号。
发布于 2010-02-20 10:01:30
也许在http://www.krysstal.com/sphertrig.html的“使用正弦规则”下的工作示例会有所帮助。
https://stackoverflow.com/questions/2301669
复制相似问题