module Geocoder::Calculations
def distance_between(point1, point2, options = {})
Use Geocoder.configure(:units => ...) to configure default units.
* :units - :mi or :km
The options hash supports:
which returns a [lat,lon] array
* a geocoded object (one which implements a +to_coordinates+ method
* a geocodable address (string)
* an array of coordinates ([lat,lon])
Geocoder methods that accept points as arguments. They can be:
The points are given in the same way that points are given to all
Takes two points and an options hash.
Distance between two points on Earth (Haversine formula).
#
def distance_between(point1, point2, options = {}) # set default options options[:units] ||= Geocoder.config.units # convert to coordinate arrays point1 = extract_coordinates(point1) point2 = extract_coordinates(point2) # convert degrees to radians point1 = to_radians(point1) point2 = to_radians(point2) # compute deltas dlat = point2[0] - point1[0] dlon = point2[1] - point1[1] a = (Math.sin(dlat / 2))**2 + Math.cos(point1[0]) * (Math.sin(dlon / 2))**2 * Math.cos(point2[0]) c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a)) c * earth_radius(options[:units]) end