簡體   English   中英

地理編碼器:如何通過距離獲得所有結果和訂單?

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

我知道如何使用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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM