确定罗盘方向从一个纬度/经度其他经度、纬度、方向

2023-09-11 00:20:25 作者:零度的忧伤

有没有人有一种算法从一个纬度/经度确定方向到另一个(伪code):

Does anyone have an algorithm to determine the direction from one lat/lon to another (pseudo-code):

CalculateHeading( lat1, lon1, lat2, long2 ) returns string heading

如果标题是如西北,西南,E等。

Where heading is e.g. NW, SW, E, etc.

基本上,我有地图上的两个点,我想考虑的方向的总体思路有50英里以东1英里北简直是东部和东北地区不

Basically, I have two points on a map and I want to get a general idea of the direction taking into account that 50 miles East and one mile North is simply East and not Northeast.

推荐答案

本网站具有基本的算法:

// in javascript, not hard to translate...
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 = Math.atan2(y, x).toDeg();

更新:对于在这里看到完整的算法研究映射数学和Javascript

这会给你一个介于0和360,然后它有一个简单的查找的只是一个问题:

That'll give you a number between 0 and 360 then it's just a matter of having a simple lookup:

var bearings = ["NE", "E", "SE", "S", "SW", "W", "NW", "N"];

var index = brng - 22.5;
if (index < 0)
    index += 360;
index = parseInt(index / 45);

return(bearings[index]);

重要的是要注意,您的轴承实际上改变,你绕地球运动。上述算法显示了你的初始的轴承,但如果您需要很长的距离,当你到达目的地(如果你只是短途旅行并[d你的轴承是显著的不同;一个几百公里]那么它很可能不会改变,足以引起人们的关注)。

It's important to note that your bearing actually changes as you move around the earth. The algorithm above shows you initial bearing, but if you're traveling a long distance, your bearing to be significantly different when you reach the destination (if you're only traveling a short distance [< a few hundred kms] then it probably won't change enough to be a concern).