简体   繁体   English

地理编码器:如何通过距离获得所有结果和订单?

[英]Geocoder: How get all results and order by distance?

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? 我知道如何使用near方法在一定范围内获得所有结果,但是如何获得所有结果并按距离排序?

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. 但是,我宁愿完全跳过半径检查,只需按距离排序所有结果。

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(point).order('distance')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM