2010-02-20 89 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