首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用两个经度/纬度点获取方向(指南针)

使用两个经度/纬度点获取方向(指南针)
EN

Stack Overflow用户
提问于 2011-12-14 18:17:04
回答 4查看 33.1K关注 0票数 21

我正在研究一种移动设备的“指南针”。我有以下几点意见:

代码语言:javascript
复制
point 1 (current location): Latitude = 47.2246, Longitude = 8.8257
point 2 (target  location): Latitude = 50.9246, Longitude = 10.2257

另外,我还有以下信息(来自我的android手机):

代码语言:javascript
复制
The compass-direction in degree, which bears to the north. 
For example, when I direct my phone to north, I get 0°

我如何创建一个“指南针”箭头来显示指向该点的方向?

这有一个数学问题吗?

编辑:好的,我找到了一个解决方案,看起来是这样的:

代码语言:javascript
复制
/**
 * 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;
}
EN

回答 4

Stack Overflow用户

发布于 2014-03-30 12:00:56

别忘了说我最终找到了答案。该应用程序用于确定公交车辆的指南针方向及其目的地。本质上,花哨的数学获取地球的曲率,找到一个角度/罗盘读数,然后将这个角度与一个通用的指南针数值进行匹配。当然,您可以只保留compassReading并将其应用为图像的旋转量。请注意,这是到终点(汽车站)的车辆方向的平均确定,这意味着它不能知道道路正在做什么(因此,这可能最适用于飞机或轮滑比赛)。

代码语言:javascript
复制
//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,busstation)可能会返回"NW“,意思是它正向西北方向移动

票数 19
EN

Stack Overflow用户

发布于 2017-08-29 10:05:27

我在math here中找到了一些有用的gps坐标公式。对于这种情况,我的解决方案如下

代码语言:javascript
复制
 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

}
票数 2
EN

Stack Overflow用户

发布于 2018-01-31 19:29:03

我不能很好地理解你的解决方案,计算斜率对我很有效。修改efwjames的答案和你的答案。这应该是-

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8502795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档