我正在爲移動設備制定「指南針」。我有以下幾點:獲取具有兩個經度/緯度點的方向(指南針)
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;
}
這工作對我來說太棒了!
不要發佈即使你不清楚的解決方案。 – Sameer 2012-09-14 15:49:36
@Sameer解決方案有什麼問題?如果nobodoy其他職位,我可以是一個。如果有人需要它,它僅供參考。得到那個「理由」的低票。無法理解。 – eav 2012-09-17 07:24:55
@eav你創建的toRad函數是什麼樣的? – gohnjanotis 2014-06-15 00:55:17