2010-02-20 88 views
0

我有一个从位置(经度)和到位置(经度,纬度)。经过计算,它应该告诉我什么是使用指南针最接近的方式。下面是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); 
    } 

错误的,如果条件: 这是greatCircleDirection功能的,如果条件的值,需要知道什么改变来解决它。

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) 
+0

example added .. – Basit 2010-02-20 10:56:43

回答

1

计算距离是不必要的;它只是增加更多的操作,并可能引入更多的数字错误。使用您的编码风格,这样的事情应该工作:

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

1

那么,你的距离计算检查。但是我看到你对初始方位的回答是(0 + 105)mod360而不是(0-105)mod360(大约),所以我怀疑你的GreatCircleDirection函数中if语句的某处出现了错误的符号。

+0

是的,你是对的,如果条件错了..你能帮我解决它,我不知道该怎么做才能解决它。 – Basit 2010-02-20 12:09:06

+0

这是条件:(0.57700585070933 <3.1415926535898和0.57700585070933 <0)或(0.57700585070933> 3.1415926535898和0.57700585070933> 0) – Basit 2010-02-20 12:15:18

+0

我应该改变对面的大小写吗?那会有用吗?但主要是,这在计算中是否准确? – Basit 2010-02-20 12:17:35