测量我用Lua跑了多远

我该如何用 lua 编程来测量我跑了多远?最好使用 GPS 位置。我有经度和纬度信息。

点赞
用户1031671
用户1031671

我会使用闭包和勾股定理来实现:

function odometer(curx, cury)
    local x = curx or 0
    local y = cury or 0
    local total = 0
    return function(newx, newy)
        local difx = math.abs(newx - x)
        local dify = math.abs(newy - y)
        total = total + math.sqrt(difx * difx + dify * dify)
        x, y = newx, newy
        return total
    end
end

在您的应用程序启动时,您将调用 odometer 并传入您当前的经度和纬度(如果没有默认为 0):

myodometer = odometer(longitude, latitude)

随后,您设置您的应用程序每过一段时间(例如 1000 毫秒)调用 myodometer 并传入您的新经度和纬度:

myodometer(newlongitude, newlatitude)

这里是 Lua 闭包的链接

2013-02-12 21:47:38