From d7e40fb1d720b459ddc1055c3a1e496c092944c7 Mon Sep 17 00:00:00 2001 From: Alex Reisner <alex@alexreisner.com> Date: Sat, 26 Mar 2011 18:58:03 -0400 Subject: [PATCH] Improve SQLite distance calculation. Multiply by .707 (sin(45)) instead of .5. --- lib/geocoder/orms/active_record.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/geocoder/orms/active_record.rb b/lib/geocoder/orms/active_record.rb index 44098b8f..b4079dae 100644 --- a/lib/geocoder/orms/active_record.rb +++ b/lib/geocoder/orms/active_record.rb @@ -157,8 +157,11 @@ module Geocoder::Orm dx = Geocoder::Calculations.longitude_degree_distance(30, options[:units] || :mi) dy = Geocoder::Calculations.latitude_degree_distance(options[:units] || :mi) - distance = "(#{dy} * ABS(#{lat_attr} - #{latitude}) / 2) + " + - "(#{dx} * ABS(#{lon_attr} - #{longitude}) / 2)" + # sin of 45 degrees = average x or y component of vector + factor = Math.sin(Math::PI / 4) + + distance = "(#{dy} * ABS(#{lat_attr} - #{latitude}) * #{factor}) + " + + "(#{dx} * ABS(#{lon_attr} - #{longitude}) * #{factor})" default_near_scope_options(latitude, longitude, radius, options).merge( :select => "#{options[:select] || '*'}, " + "#{distance} AS distance" + -- GitLab