Created
September 6, 2012 11:19
-
-
Save tagliala/3655017 to your computer and use it in GitHub Desktop.
distance from
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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