地理编码器:如何通过距离获得所有结果和订单?
我知道如何使用near
方法在一定范围内获得所有结果,但是如何获得所有结果并按距离排序?
我知道我可以这样做:
Location.near(my_location, 999999, order: 'distance')
但是,我宁愿完全跳过半径检查,只需按距离排序所有结果。
我遇到了同样的问题,最后我将这个范围添加到我的地理编码类:
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
这将允许你做A.with_distance_to(point).order('distance')