diff --git a/lib/geocoder/calculations.rb b/lib/geocoder/calculations.rb index b617bad206abfacbd543ae825545830036ce070f..a3c69ce8808eba4179b32bf6f03ee6a833dcd988 100644 --- a/lib/geocoder/calculations.rb +++ b/lib/geocoder/calculations.rb @@ -22,7 +22,10 @@ module Geocoder options[:units] ||= :mi # define conversion factors - conversions = { :mi => 3956, :km => 6371 } + conversions = { + :mi => earth_radius, + :km => earth_radius / km_in_mi + } # convert degrees to radians lat1 = to_radians(lat1) @@ -113,6 +116,13 @@ module Geocoder (radians * 180.0) / Math::PI end + ## + # Radius of the earth, in miles. + # + def earth_radius + 3956 + end + ## # Conversion factor: km to mi. # diff --git a/lib/geocoder/orms/active_record.rb b/lib/geocoder/orms/active_record.rb index 2633fb27e0a55d3f2eea41efc3581de010568385..25d44ef3f9a6c8e80c77eecd33b44c8e42bbf7c1 100644 --- a/lib/geocoder/orms/active_record.rb +++ b/lib/geocoder/orms/active_record.rb @@ -93,7 +93,7 @@ module Geocoder::Orm "(SIN(RADIANS(#{latitude})) * " + "COS(RADIANS(#{lat_attr})) * " + "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}) * " + "PI() / 180 / 2), 2) + COS(#{latitude} * PI() / 180) * " + "COS(#{lat_attr} * PI() / 180) * " +