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

发布于 2024-12-21 04:05:08 字数 1252 浏览 3 评论 0原文

我正在为移动设备开发“指南针”。我有以下几点:

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, which 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;
}

I'm working on a "compass" for a mobile-device. I have the following points:

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

Also I have the following information (from my android-phone):

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

How can I create a "compass-like" arrow which shows me the direction to the point?

Is there a mathematic-problem for this?

EDIT: Okay I found a solution, it looks like this:

/**
 * 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花开半夏魅人心 2024-12-28 04:05:08

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
}

IE:
车辆轴承(mybus、busstation)
可能返回“NW”意味着它向西北方向行驶

O forgot to say I found the answer eventually. The application is to determine compass direction of a transit vehicle and its destination. Essentially, fancy math for acquiring curvature of Earth, finding an angle/compass reading, and then matching that angle with a generic compass value. You could of course just keep the compassReading and apply that as an amount of rotation for your image. Please note this is an averaged determination of the vehicle direction to the end point (bus station) meaning it can't know what the road is doing (so this probably best applies to airplanes or roller derby).

//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
}

ie:
vehicleBearing(mybus, busstation)
might return "NW" means its travelling northwesterly

£噩梦荏苒 2024-12-28 04:05:08

我在数学此处找到了一些有用的 GPS 坐标公式。
对于这种情况,这是我的解决方案

 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

}

I found some useful gps coordinates formula in math here.
For this case, here my solution

 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

}
分分钟 2024-12-28 04:05:08

我无法很好地理解你的解决方案,计算斜率对我有用。
修改 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

I couldn't understand your solution well, calculating the slope worked for me.
To modify on efwjames's and your answer. This should do -

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
伴我心暖 2024-12-28 04:05:08

您需要计算起点和终点之间的欧几里得向量,然后计算其角度(比方说相对于正 X),这将是您想要旋转箭头的角度。

You'd need to calculate an Euclidean vector between your start point and end point, then calculate its angle (let's say relative to positive X) which would be the angle you want to rotate your arrow by.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文