Class: Mongoid::Geo::Haversine

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/geo/haversine.rb

Constant Summary collapse

RADIAN_PER_DEGREE =

EARTH_RADIUS = 6371 # kilometers

Math::PI / 180.0

Class Method Summary collapse

Class Method Details

.distance(lat1, lng1, lat2, lng2) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mongoid/geo/haversine.rb', line 8

def self.distance(lat1, lng1, lat2, lng2)
   lat1_radians = lat1 * RADIAN_PER_DEGREE
   lat2_radians = lat2 * RADIAN_PER_DEGREE

   distance_lat = (lat2-lat1) * RADIAN_PER_DEGREE
   distance_lng = (lng2-lng1) * RADIAN_PER_DEGREE

   a = Math.sin(distance_lat/2)**2 + Math.cos(lat1_radians) * Math.cos(lat2_radians) * Math.sin(distance_lng/2) ** 2
   c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
#         EARTH_RADIUS * c #
   c
end