module Geocoder::Calculations
def bearing_between(point1, point2, options = {}, *args)
Based on: http://www.movable-type.co.uk/scripts/latlong.html
the same latitude)
is less confusing (returns due east or west when given two points with
(one along a great circle) but the linear method is the default as it
the spherical method is "correct" in that it returns the shortest path
* :method - :linear (default) 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 = {}, *args) if args.size > 0 warn "DEPRECATION WARNING: Instead of passing lat1/lon1/lat2/lon2 as separate arguments to the bearing_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 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