2011-12-14 98 views
17

我正在爲移動設備制定「指南針」。我有以下幾點:獲取具有兩個經度/緯度點的方向(指南針)

point 1 (current location): Latitude = 47.2246, Longitude = 8.8257 
point 2 (target location): Latitude = 50.9246, Longitude = 10.2257 

而且我有以下的信息(從我的Android手機):

The compass-direction in degree, wich bears to the north. 
For example, when I direct my phone to north, I get 0° 

如何創建一個「指南針狀」箭頭至極顯示我的方向到了那一點?

這是否存在數學問題?

謝謝!

編輯:好吧,我發現了一個解決方案,它看起來像這樣:

/** 
* Params: lat1, long1 => Latitude and Longitude of current point 
*   lat2, long2 => Latitude and Longitude of target point 
*   
*   headX  => x-Value of built-in phone-compass 
* 
* Returns the degree of a direction from current point to target point 
* 
*/ 
function getDegrees(lat1, long1, lat2, long2, headX) { 

    var dLat = toRad(lat2-lat1); 
    var dLon = toRad(lon2-lon1); 

    lat1 = toRad(lat1); 
    lat2 = toRad(lat2); 

    var y = Math.sin(dLon) * Math.cos(lat2); 
    var x = Math.cos(lat1)*Math.sin(lat2) - 
      Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon); 
    var brng = toDeg(Math.atan2(y, x)); 

    // fix negative degrees 
    if(brng<0) { 
     brng=360-Math.abs(brng); 
    } 

    return brng - headX; 
} 

這工作對我來說太棒了!

+1

不要發佈即使你不清楚的解決方案。 – Sameer 2012-09-14 15:49:36

+0

@Sameer解決方案有什麼問題?如果nobodoy其他職位,我可以是一個。如果有人需要它,它僅供參考。得到那個「理由」的低票。無法理解。 – eav 2012-09-17 07:24:55

+0

@eav你創建的toRad函數是什麼樣的? – gohnjanotis 2014-06-15 00:55:17

回答

-1

您需要計算起點和終點之間的歐幾里德向量,然後計算其角度(假設相對於正X),這將是您想旋轉箭頭的角度。

14

O忘了說我最終找到了答案。應用程序是確定公交車輛及其目的地的羅盤方向。從本質上講,花哨的數學可以獲得地球曲率,找到角度/羅盤讀數,然後將該角度與通用指南針值進行匹配。你當然可以保持羅盤閱讀並將其應用爲圖像的旋轉量。請注意,這是平均確定車輛到終點(巴士站)的方向,這意味着它無法知道道路在幹什麼(所以這可能最適用於飛機或滾筒德比)。

//example obj data containing lat and lng points 
//stop location - the radii end point 
endpoint.lat = 44.9631; 
endpoint.lng = -93.2492; 

//bus location from the southeast - the circle center 
startpoint.lat = 44.95517; 
startpoint.lng = -93.2427; 

function vehicleBearing(endpoint, startpoint) { 
    endpoint.lat = x1; 
    endpoint.lng = y1; 
    startpoint.lat = x2; 
    startpoint.lng = y2; 

    var radians = getAtan2((y1 - y2), (x1 - x2)); 

    function getAtan2(y, x) { 
     return Math.atan2(y, x); 
    }; 

    var compassReading = radians * (180/Math.PI); 

    var coordNames = ["N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"]; 
    var coordIndex = Math.round(compassReading/45); 
    if (coordIndex < 0) { 
     coordIndex = coordIndex + 8 
    }; 

    return coordNames[coordIndex]; // returns the coordinate value 
} 

即: vehicleBearing(mybus,站點巴士) 可能返回 「NW」 是指其行進西北

1

我發現一些有用的GPS在數學here座標公式。 對於這種情況,在這裏我的解決方案

private double getDirection(double lat1, double lng1, double lat2, double lng2) { 

    double PI = Math.PI; 
    double dTeta = Math.log(Math.tan((lat2/2)+(PI/4))/Math.tan((lat1/2)+(PI/4))); 
    double dLon = Math.abs(lng1-lng2); 
    double teta = Math.atan2(dLon,dTeta); 
    double direction = Math.round(Math.toDegrees(teta)); 
    return direction; //direction in degree 

} 
0

我不明白您的解決方案很好,計算的斜率爲我工作。 修改efwjames和你的答案。這應該可以 -

import math 
def getDegrees(lat1, lon1, lat2, lon2,head): 
    dLat = math.radians(lat2-lat1) 
    dLon = math.radians(lon2-lon1) 
    bearing = math.degrees(math.atan2(dLon, dLat)) 
    return head-bearing