I have 2 questions regarding the gem 'geokit-rails'
This is my sample code
class Outlet < ActiveRecord::Base
acts_as_mappable :lat_column_name => :address_latitude,
:lng_column_name => :address_longitude,
:default_units => :kms
end
I cant use the find(:all)
sample Outlet.find(:all, :origin =>[32.951613,-96.958444], :within=>10)
it returns ActiveRecord::RecordNotFound: Couldn't find all Outlets with 'id': (all, {:origin=>[32.951613, -96.958444], :within=>10})
I have also tested on this query Outlet.find(:all)
it returns ActiveRecord::RecordNotFound: Couldn't find Outlet with 'id'=all
Distance. I dont really understand how does this work. Should we add distance column into the the outlet model ? If yes, whats the column type? And is that possible to return the record with the distance value based on the given lat lng ? Outlet.by_distance(:origin =>[32.951613, -96.958444])
it will return the records with the distance to that point. (2kms, 3kms, 4km, ...)
find is an ActiveRecord method. It has nothing to do with Geokit . It takes model id
such as 1
for the first Outlet you created or 2
for the second Outlet. You used Outlet.find(:all)
so, ActiveRecord complained that it could not find any Outlet with id all
.
I think what you actually wanted to do is
Outlet.within(10, :origin => [32.951613,-96.958444])
Note that you can also pass an object for origin, for example,
@the_latest_outlet = Outlet.last @outlets_nearby_the_latest_outlet = Outlet.within(10, origin: @the_latest_outlet)
No, you don't have to add a column. The field distance
is added to ActiveRecord instances automatically. I still haven't found any use of the parameter :distance_field_name
. My best guess is that if your model has a field named distance
, you should use distance_field_name
parameter to avoid the conflict.
2.1. If you want to measure distance between 2 points, you need distance_to as this example:
a = Outlet.find(1) b = Outlet.find(2) distance_from_a_to_b = a.distance_to(b)
2.2. by_distance
is to get all your instances sorted by distance.
a_place_to_compare = [lat, lng] all_outlets_sorted_by_distance_from_the_place = Outlet.by_distance(a_place_to_compare)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.