Skip to content

Instantly share code, notes, and snippets.

@tagliala
Created September 6, 2012 11:19
Show Gist options
  • Save tagliala/3655017 to your computer and use it in GitHub Desktop.
Save tagliala/3655017 to your computer and use it in GitHub Desktop.
distance from
# http://www.movable-type.co.uk/scripts/latlong.html
def distance_from(place, unit = :km)
radius = (unit == :km) ? 6371 : 3959
dLat = (place.source[:lat] - source[:lat]) * Math::PI / 180
dLon = (place.source[:lng] - source[:lng]) * Math::PI / 180
lat1 = source[:lat] * Math::PI / 180
lat2 = place.source[:lat] * Math::PI / 180
a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
radius * c
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment