我正在为移动设备制定“指南针”。我有以下几点:获取具有两个经度/纬度点的方向(指南针)
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