Skip to content
Snippets Groups Projects
Commit cf98f916 authored by Alex Reisner's avatar Alex Reisner
Browse files

Move Earth's radius to separate method.

parent 3e7a3a06
No related branches found
No related tags found
No related merge requests found
...@@ -22,7 +22,10 @@ module Geocoder ...@@ -22,7 +22,10 @@ module Geocoder
options[:units] ||= :mi options[:units] ||= :mi
# define conversion factors # define conversion factors
conversions = { :mi => 3956, :km => 6371 } conversions = {
:mi => earth_radius,
:km => earth_radius / km_in_mi
}
# convert degrees to radians # convert degrees to radians
lat1 = to_radians(lat1) lat1 = to_radians(lat1)
...@@ -113,6 +116,13 @@ module Geocoder ...@@ -113,6 +116,13 @@ module Geocoder
(radians * 180.0) / Math::PI (radians * 180.0) / Math::PI
end end
##
# Radius of the earth, in miles.
#
def earth_radius
3956
end
## ##
# Conversion factor: km to mi. # Conversion factor: km to mi.
# #
......
...@@ -93,7 +93,7 @@ module Geocoder::Orm ...@@ -93,7 +93,7 @@ module Geocoder::Orm
"(SIN(RADIANS(#{latitude})) * " + "(SIN(RADIANS(#{latitude})) * " +
"COS(RADIANS(#{lat_attr})) * " + "COS(RADIANS(#{lat_attr})) * " +
"COS(RADIANS(#{lon_attr} - #{longitude}))))) + 360) % 360" "COS(RADIANS(#{lon_attr} - #{longitude}))))) + 360) % 360"
distance = "3956 * 2 * ASIN(SQRT(" + distance = "#{Geocoder::Calculations.earth_radius} * 2 * ASIN(SQRT(" +
"POWER(SIN((#{latitude} - #{lat_attr}) * " + "POWER(SIN((#{latitude} - #{lat_attr}) * " +
"PI() / 180 / 2), 2) + COS(#{latitude} * PI() / 180) * " + "PI() / 180 / 2), 2) + COS(#{latitude} * PI() / 180) * " +
"COS(#{lat_attr} * PI() / 180) * " + "COS(#{lat_attr} * PI() / 180) * " +
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment