Geocoder:如何通过距离获得所有结果和顺序?

时间:2021-01-29 20:11:11

I know how to use the near method to get all results within a certain radius, but how do I get all results and order by distance?

我知道如何使用近方法在一定半径内得到所有结果,但如何得到所有结果和距离的顺序?

I know I could do something like this:

我知道我可以这样做:

Location.near(my_location, 999999, order: 'distance')

However, I would rather skip the radius check altogether and simply get all results sorted by distance.

但是,我宁愿完全跳过半径检查,而只是简单地按距离排序所有结果。

1 个解决方案

#1


4  

I have had the same issue, and I ended up adding this scope to my geocoded class:

我也遇到过同样的问题,最后我把这个范围添加到我的地理编码类中:

class A
  extend Geocoder::Model::ActiveRecord
  reverse_geocoded_by :latitude, :longitude

  scope :with_distance_to, ->(point) { select("#{table_name}.*").select("(#{distance_from_sql(point)}) as distance") }
end

This will allow you to do A.with_distance_to(point).order('distance')

这将允许您执行A.with_distance_to(点).order('distance')

#1


4  

I have had the same issue, and I ended up adding this scope to my geocoded class:

我也遇到过同样的问题,最后我把这个范围添加到我的地理编码类中:

class A
  extend Geocoder::Model::ActiveRecord
  reverse_geocoded_by :latitude, :longitude

  scope :with_distance_to, ->(point) { select("#{table_name}.*").select("(#{distance_from_sql(point)}) as distance") }
end

This will allow you to do A.with_distance_to(point).order('distance')

这将允许您执行A.with_distance_to(点).order('distance')