[英]Geokit Rails distance and find(:all)
I have 2 questions regarding the gem 'geokit-rails'
我有两个关于
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) 我不能使用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})
示例
Outlet.find(:all, :origin =>[32.951613,-96.958444], :within=>10)
它返回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
我也测试了这个查询
Outlet.find(:all)
它返回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 ?
并且可以根据给定的lat lng返回带有距离值的记录吗?
Outlet.by_distance(:origin =>[32.951613, -96.958444])
it will return the records with the distance to that point. Outlet.by_distance(:origin =>[32.951613, -96.958444])
它将返回与该点距离的记录。 (2kms, 3kms, 4km, ...) (2公里,3公里,4公里......)
find is an ActiveRecord method. find是一个ActiveRecord方法。 It has nothing to do with Geokit .
它与Geokit无关 。 It takes model
id
such as 1
for the first Outlet you created or 2
for the second Outlet. 它为您创建的第一个Outlet或第二个Outlet的
2
个模型id
例如1
。 You used Outlet.find(:all)
so, ActiveRecord complained that it could not find any Outlet with id all
. 你使用
Outlet.find(:all)
所以,ActiveRecord抱怨它找不到任何id为all
Outlet。
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. 字段
distance
自动添加到ActiveRecord实例。 I still haven't found any use of the parameter :distance_field_name
. 我还没有找到任何使用参数
: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. 我最好的猜测是,如果你的模型有一个名为
distance
的字段,你应该使用distance_field_name
参数来避免冲突。
2.1. 2.1。 If you want to measure distance between 2 points, you need distance_to as this example:
如果要测量2点之间的距离,则需要distance_to作为此示例:
a = Outlet.find(1) b = Outlet.find(2) distance_from_a_to_b = a.distance_to(b)
2.2. 2.2。
by_distance
is to get all your instances sorted by distance. by_distance
是按距离排序所有实例。
a_place_to_compare = [lat, lng] all_outlets_sorted_by_distance_from_the_place = Outlet.by_distance(a_place_to_compare)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.