2013-08-28 239 views
2

我一直在試圖弄清楚兩個緯度座標之間的方位,但是我很難理解這個概念。計算兩個緯度座標之間的距離/方位

我去過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)讓我感到困惑。

如果我的初始LAT LON是:

緯度= -33.8830555556 經度= 151.216666667

和我的目的地lat和經度是:

緯度= 22.25 經度= 114.1667

哪些變量需要匹配哪些經緯度?

dLon變量是指縱向兩點之間的距離嗎?

非常感謝!

回答

3

根據JavaScript代碼,lat1是初始點的緯度,lat2是目標點的緯度。

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

此外,Lua中的數學庫被稱爲math,而不是Math

+0

感謝那些得到它,這有一定道理。 – user2609216

1

試試這個

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

相關問題