簡體   English   中英

計算兩個緯度坐標之間的距離/軸承

[英]Calculating the distance/bearing between two lat lon co ordinates

我一直在嘗試計算兩個緯度坐標之間的關系,但是在理解這個概念時遇到了麻煩。

我去過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變量是縱向參考兩點之間的距離嗎?

謝謝一群!

基於JavaScript代碼, lat1是初始點的緯度,而lat2是目標點的緯度。

請注意,所有緯度和經度都必須以弧度為單位; 使用math.rad()進行轉換。

Lua中的數學庫也稱為math ,而不是Math

嘗試這個

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

這是我已經完成的功能,可以完美地工作。 在X-Plane Flight Simulator中經過嚴格測試。 最后,地球半徑除以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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM