Class: GeoDistance::Haversine

Inherits:
DistanceFormula show all
Defined in:
lib/geo-distance/formula/haversine.rb

Class Method Summary collapse

Methods inherited from DistanceFormula

geo_distance, get_points, get_units, #initialize

Constructor Details

This class inherits a constructor from GeoDistance::DistanceFormula

Class Method Details

.distance(*args) ⇒ Object

given two lat/lon points, compute the distance between the two points using the haversine formula

the result will be a Hash of distances which are key'd by 'mi','km','ft', and 'm'


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/geo-distance/formula/haversine.rb', line 41

def self.distance *args
  begin
    from, to, units = get_points(args)
    
    lat1, lon1, lat2, lon2 = [from.lat, from.lng, to.lat, to.lng]               
    dlon = lon2 - lon1
    dlat = lat2 - lat1

    a = (Math.sin(dlat.rpd/2))**2 + Math.cos(lat1.rpd) * Math.cos((lat2.rpd)) * (Math.sin(dlon.rpd/2))**2
    c = (2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a)))
    c = c.to_deg
            
    units ? c.radians_to(units) : c
  rescue Errno::EDOM
    0.0        
  end
end