module Geocoder::Calculations
def bearing_between(point1, point2, options = {})
Based on: http://www.movable-type.co.uk/scripts/latlong.html
Use Geocoder.configure(:distances => ...) to configure calculation method.
(returns due east or west when given two points with the same latitude).
(one along a great circle) but the linear method is less confusing
the spherical method is "correct" in that it returns the shortest path
* :method - :linear or :spherical;
ways of specifying the points. Also accepts an options hash:
See Geocoder::Calculations.distance_between for
Returns a number of degrees from due north (clockwise).
Bearing between two points on Earth.
#
def bearing_between(point1, point2, options = {}) # set default options options[:method] ||= Geocoder.config.distances options[:method] = :linear unless options[:method] == :spherical # 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] case options[:method] when :linear y = dlon x = dlat when :spherical y = Math.sin(dlon) * Math.cos(point2[0]) x = Math.cos(point1[0]) * Math.sin(point2[0]) - Math.sin(point1[0]) * Math.cos(point2[0]) * Math.cos(dlon) end bearing = Math.atan2(x,y) # Answer is in radians counterclockwise from due east. # Convert to degrees clockwise from due north: (90 - to_degrees(bearing) + 360) % 360 end