This return the distance between two locations that are nearby, it works fine unless you have markers all over the world. I used this when I played around with proximity notifications (trigger alerts, timers, notifications based on if the user is within distance of a location).
[lua]
local rad = math.rad
local cos = math.cos
local sqrt = math.sqrt
local earthRadiusInMeters = 6378137
– Equirectangular approximation, works for short distances
function distanceApproximation(lat1, long1, lat2, long2)
if not lat1 or not long1 or not lat2 or not long2 then
print(" ===> Missing latitudes and longitudes")
return
end
local l1, lng1 = rad(lat1), rad(long1)
local l2, lng2 = rad(lat2), rad(long2)
local x = (lng2-lng1) * cos((l1+l2)/2)
local y = (l2-l1)
local d = sqrt(x * x + y * y) * earthRadiusInMeters
print(" ===> distanceApproximation: “…d…” meters")
return d
end
[/lua]