2011-12-14 99 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