Module: ServerSideGoogleMaps::GeoMath

Defined in:
lib/server-side-google-maps/geo_math.rb

Constant Summary collapse

RADIUS_OF_EARTH =

metres

6367000

Class Method Summary collapse

Class Method Details

.latlng_distance(pt1, pt2) ⇒ Object

Returns the distance (in m) between two [lat,lng] points with the Haversine formula



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/server-side-google-maps/geo_math.rb', line 6

def self.latlng_distance(pt1, pt2)
  lat1 = pt1[0] * Math::PI / 180
  lon1 = pt1[1] * Math::PI / 180
  lat2 = pt2[0] * Math::PI / 180
  lon2 = pt2[1] * Math::PI / 180
  dlon = lon2 - lon1
  dlat = lat2 - lat1
  a = Math.sin(dlat/2)**2 + Math.cos(lat1) * Math.cos(lat2) * Math.sin(dlon/2)**2
  c = 2 * Math.asin(min(1.0, Math.sqrt(a)))
  (RADIUS_OF_EARTH * c).to_i
end