【问题标题】:Calculating the distance/bearing between two lat lon co ordinates计算两个纬度坐标之间的距离/方位
【发布时间】:2013-09-01 03:56:18
【问题描述】:

我一直在尝试找出两个纬度坐标之间的方位,但无法理解这个概念。

我去过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变量是指纵向两点之间的距离吗?

非常感谢!

【问题讨论】:

    标签: lua distance latitude-longitude bearing


    【解决方案1】:

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

    请注意,所有纬度和经度必须以弧度表示;使用math.rad() 进行转换。

    Lua 中的数学库也称为 math,而不是 Math。

    【讨论】:

    • 谢谢你知道了,确实有道理。
    【解决方案2】:

    试试这个

    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) = ~287km

    【讨论】:

      【解决方案3】:

      这是我制作的一个功能,效果很好。在 X-Plane 飞行模拟器中经过严格测试。最后是地球半径除以 1852,因此函数返回距离为海里。

      function GC_distance_calc(lat1, lon1, lat2, lon2)
      
      --This function returns great circle distance between 2 points.
      --Found here: http://bluemm.blogspot.gr/2007/01/excel-formula-to-calculate-distance.html
      --lat1, lon1 = the coords from start position (or aircraft's) / lat2, lon2 coords of the target waypoint.
      --6371km is the mean radius of earth in meters. Since X-Plane uses 6378 km as radius, which does not makes a big difference,
      --(about 5 NM at 6000 NM), we are going to use the same.
      --Other formulas I've tested, seem to break when latitudes are in different hemisphere (west-east).
      
      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
      

      【讨论】:

        猜你喜欢
        • 2011-09-16
        • 1970-01-01
        • 2015-11-05
        • 2017-06-26
        • 2018-06-11
        • 2020-02-12
        • 2018-06-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多