Skip to content

Commit

Permalink
Merge pull request #72 from jensb/master
Browse files Browse the repository at this point in the history
Update *_distance_sql to work with customized latitude and longitude attribute names
  • Loading branch information
Michael Noack committed Mar 11, 2015
2 parents a2a36d2 + 6934bff commit 3c7659b
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/geokit-rails/acts_as_mappable.rb
Expand Up @@ -315,22 +315,33 @@ def substitute_distance_in_where_values(arel, origin, units=default_units, formu
# Returns the distance SQL using the spherical world formula (Haversine). The SQL is tuned
# to the database in use.
def sphere_distance_sql(origin, units)
lat = deg2rad(origin.lat)
lng = deg2rad(origin.lng)
# "origin" can be a Geokit::LatLng (with :lat and :lng methods), e.g.
# when using geo_scope or it can be an ActsAsMappable with customized
# latitude and longitude methods, e.g. when using distance_sql.
lat = deg2rad(get_lat(origin))
lng = deg2rad(get_lng(origin))
multiplier = units_sphere_multiplier(units)

adapter.sphere_distance_sql(lat, lng, multiplier) if adapter
end

# Returns the distance SQL using the flat-world formula (Phythagorean Theory). The SQL is tuned
# to the database in use.
def flat_distance_sql(origin, units)
lat_degree_units = units_per_latitude_degree(units)
lng_degree_units = units_per_longitude_degree(origin.lat, units)

lng_degree_units = units_per_longitude_degree(get_lat(origin), units)
adapter.flat_distance_sql(origin, lat_degree_units, lng_degree_units)
end

def get_lat(origin)
origin.respond_to?(:lat) ? origin.lat \
: origin.send(:"#{lat_column_name}")
end

def get_lng(origin)
origin.respond_to?(:lng) ? origin.lng \
: origin.send(:"#{lng_column_name}")
end

end # ClassMethods

# this is the callback for auto_geocoding
Expand All @@ -339,8 +350,8 @@ def auto_geocode_address
geo=Geokit::Geocoders::MultiGeocoder.geocode(address)

if geo.success
self.send("#{lat_column_name}=", geo.lat)
self.send("#{lng_column_name}=", geo.lng)
self.send("#{lat_column_name}=", geo.send(:"#{lat_column_name}"))
self.send("#{lng_column_name}=", geo.send(:"#{lng_column_name}"))
else
errors.add(auto_geocode_field, auto_geocode_error_message)
end
Expand Down

0 comments on commit 3c7659b

Please sign in to comment.