计算两个纬度和经度坐标之间的距离和方位角。

我一直在尝试计算两个经纬度坐标之间的方位角,但有些难以理解这个概念。

我已经访问了http://www.movable-type.co.uk/scripts/latlong.html,并已经能够将距离代码更改为适用于lua,但是段落下的方位角代码使我有些困惑。

local y = Math.sin(dLon) * Math.cos(lat2)
local x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon)
local brng = Math.atan2(y, x).toDeg()

变量的命名(lat1、lat2、dLon)使我感到困惑。

如果我的初始经纬度是:

纬度= -33.8830555556 经度= 151.216666667

我的目标经纬度是:

纬度=22.25 经度=114.1667

哪些变量需要匹配到哪些经度和纬度?

dLon变量是否指两点经度之间的距离?

非常感谢!

点赞
用户107090
用户107090

根据 JavaScript 代码,lat1 是初始点的纬度,lat2 是目标点的纬度。

注意所有纬度和经度都必须以弧度为单位;使用 math.rad() 进行转换。

此外,Lua 中的数学库称为 math,而不是 Math

2013-08-29 00:19:12
用户407731
用户407731
尝试这个

local function geo_distance(lat1, lon1, lat2, lon2) if lat1 == nil or lon1 == nil or lat2 == nil or lon2 == nil then return nil end local dlat = math.rad(lat2-lat1) local dlon = math.rad(lon2-lon1) local sin_dlat = math.sin(dlat/2) local sin_dlon = math.sin(dlon/2) local a = sin_dlat * sin_dlat + math.cos(math.rad(lat1)) * math.cos(math.rad(lat2)) * sin_dlon * sin_dlon local c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) -- 6378 km is the earth's radius at the equator. -- 6357 km would be the radius at the poles (earth isn't a perfect circle). -- Thus, high latitude distances will be slightly overestimated -- To get miles, use 3963 as the constant (equator again) local d = 6378 * c return d end

```

输入的坐标是以度为单位的,所以 geo_distance(30.19, 71.51, 31.33, 74.21) = 约287公里

2014-01-17 19:07:10
用户5042448
用户5042448

下面是一段我创建并运转完美的函数。在 X-Plane 飞行模拟器上进行了严格测试。在函数结尾,地球半径被除以 1852,以便函数返回海里数的距离。

function GC_distance_calc(lat1, lon1, lat2, lon2)

--该函数返回2个点之间的大圆距离。
--来源于:http://bluemm.blogspot.gr/2007/01/excel-formula-to-calculate-distance.html
--lat1, lon1 = 起始点的坐标(或飞机的)/ lat2, lon2是目标航点的坐标。
--6371km是地球的平均半径(米)。由于 X-Plane 使用的半径是 6378 公里,并没有太大区别,
--(在 6000 公里处约为5海里),因此我们将使用相同的值。
--其他我测试过的公式,当纬度处于不同的半球(东西)时,似乎会出现错误。

local distance = math.acos(math.cos(math.rad(90-lat1))*math.cos(math.rad(90-lat2))+
    math.sin(math.rad(90-lat1))*math.sin(math.rad(90-lat2))*math.cos(math.rad(lon1-lon2))) * (6378000/1852)

return distance

end
2018-07-20 17:59:40