Skip to content
Snippets Groups Projects
Commit 892eab89 authored by Alex Reisner's avatar Alex Reisner
Browse files

Add Geocoder.distance_between class method.

parent b082749e
No related branches found
No related tags found
No related merge requests found
......@@ -40,6 +40,29 @@ module Geocoder
end
end
##
# Calculate the distance between two points (Haversine formula).
#
def self.distance_between(lat1, lon1, lat2, lon2, options = {})
# set default options
options[:units] ||= :mi
# define available units
units = { :mi => 3956, :km => 6371 }
# convert degrees to radians
lat1 *= Math::PI / 180
lon1 *= Math::PI / 180
lat2 *= Math::PI / 180
lon2 *= Math::PI / 180
dlat = (lat1 - lat2).abs
dlon = (lon1 - lon2).abs
a = (Math.sin(dlat / 2))**2 + Math.cos(lat1) *
(Math.sin(dlon / 2))**2 * Math.cos(lat2)
c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a))
c * units[options[:units]]
end
##
# Find all records within a radius (in miles) of the given point.
# Taken from excellent tutorial at:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment