地理编码器:如何按距离获取所有结果和排序? [英] Geocoder: How get all results and order by distance?

查看:43
本文介绍了地理编码器:如何按距离获取所有结果和排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用near方法获取一定半径内的所有结果,但是如何获取所有结果并按距离排序?

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.

推荐答案

我遇到了同样的问题,我最终将此范围添加到我的地理编码类中:

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

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

这篇关于地理编码器:如何按距离获取所有结果和排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆