Skip to content

Instantly share code, notes, and snippets.

@mikz
Created May 29, 2014 09:45
Show Gist options
  • Save mikz/2e0d4d94ed7aa39f3c5c to your computer and use it in GitHub Desktop.
Save mikz/2e0d4d94ed7aa39f3c5c to your computer and use it in GitHub Desktop.
local geo = {}
-- ~radius of the earth in miles
geo.radius = 3958.75587
-- miles between point a and point b
function geo.distance(lat1, lng1, lat2, lng2)
local rlat = lat1*math.pi/180;
local rlng = lng1*math.pi/180;
local rlat2 = lat2*math.pi/180;
local rlng2 = lng2*math.pi/180;
if (rlat == rlat2 and rlng == rlng2) then
return 0
else
-- Spherical Law of Cosines
return geo.radius*math.acos(math.sin(rlat)*math.sin(rlat2)
+math.cos(rlng-rlng2)*math.cos(rlat)*math.cos(rlat2))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment