module Geocoder::Calculations
def distance_between(point1, point2, options = {}, *args)
* :units - :mi (default) 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 = {}, *args) if args.size > 0 warn "DEPRECATION WARNING: Instead of passing lat1/lon1/lat2/lon2 as separate arguments to the distance_between method, please pass two two-element arrays: [#{point1},#{point2}], [#{options}, #{args.first}]. The old argument format will not be supported in Geocoder v.1.0." point1 = [point1, point2] point2 = [options, args.shift] options = args.shift || {} end # set default options options[:units] ||= :mi # 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