Skip to content

Instantly share code, notes, and snippets.

@carlzulauf
Created February 2, 2012 16:57
Show Gist options
  • Save carlzulauf/1724557 to your computer and use it in GitHub Desktop.
Save carlzulauf/1724557 to your computer and use it in GitHub Desktop.
Ruby haversine distance method, in miles
def geodist(a, b)
rads = Math::PI/180
# 7926.3352 is diameter of earth in miles
7926.3352 * Math.asin(
Math.sqrt(
Math.sin( (b[1] - a[1])*rads / 2 )**2 *
Math.cos( a[0] * rads ) *
Math.cos( b[0] * rads ) +
Math.sin( (b[0] - a[0])*rads / 2 )**2
)
)
end
puts geodist([41.2966716,-96.0750074],[41.133382,-95.96915])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment