Skip to content

Instantly share code, notes, and snippets.

@andersosthus
Created April 11, 2012 23:40
Show Gist options
  • Save andersosthus/2363499 to your computer and use it in GitHub Desktop.
Save andersosthus/2363499 to your computer and use it in GitHub Desktop.
Ruby Haversine
def haversine(lat1, long1, lat2, long2)
dtor = Math::PI/180
r = 6378.14*1000
rlat1 = lat1 * dtor
rlong1 = long1 * dtor
rlat2 = lat2 * dtor
rlong2 = long2 * dtor
dlon = rlong1 - rlong2
dlat = rlat1 - rlat2
a = power(Math::sin(dlat/2), 2) + Math::cos(rlat1) * Math::cos(rlat2) * power(Math::sin(dlon/2), 2)
c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1-a))
d = r * c
return d
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment