Ruby Geocoder如何设置最小近距离 [英] Ruby Geocoder how to set a minimum .near distance

查看:58
本文介绍了Ruby Geocoder如何设置最小近距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Alex Reisner的 Ruby Geocoder 并发现.near方法非常有用.我们有一个问题是如何设置它,以便我们可以搜索到距原点的距离x与距离y之间的对象(目前,.near方法仅设置它看起来的最大距离): 此处的文档

Using Alex Reisner's Ruby Geocoder and finding the .near method very useful. One question we have is how can we set it so that we can search for objects between distance x and distance y from the origin (at the moment the .near method only sets the max distance it seems): documentation here

我们正在寻找类似的东西:

We're looking for something like:

places = Place.near("Washington", min_distance, max_distance).reorder("score")

或某种修改查询的方式,使我们无需使用.reorder(我们将其用于对另一个得分"字段进行排序)即可获得相同的效果

or some way of modifying the query so that we get the same effect without using .reorder (which we're using to sort on another "score" field)

我们想要的最终结果是能够按分数排序从原点起x到y英里之间的地点.

The end result we want is the ability to have places ordered by score which are between x and y miles from the origin.

推荐答案

AFAIK,它不是本机实现的.

AFAIK it is not natively implemented.

如果使用ActiveRecord,则可以尝试使用Geocoder的私有类方法(请参见第105至147行)来滚动自己的反向"方法,其算法应大致相同:

If using ActiveRecord, you can try to use Geocoder's private class methods (see from line 105 to 147) to roll your own "reverse" method, with an algorithm that should be more or less :

  • 使用Geocoder::Calculations#bounding_box
  • 计算最小距离查询的边界框
  • 构建SQL以在边界框中并使用#distance_sql(lat,lon,options)> = min_distance
  • 查找所有记录
  • calculate bounding box for mininum distance query with Geocoder::Calculations#bounding_box
  • build SQL to find all records in bounding box AND with #distance_sql(lat,lon,options) >= min_distance

将此逻辑填充到where范围内,将其链接到near,然后瞧瞧!如果您感到懒惰,甚至可以复制/粘贴整个方法,然后更改<=谓词.

stuff this logic inside a where scope, chain it to near, and voilà! If you feel lazy you can even just copy/paste the whole method and just change the <= predicate.

顺便提一下,最好在geocoder的github上提出功能请求,因为我认为可以快速为他们实现.

On a side note, it would be great to file a feature request at geocoder's github, as i think it would be quick to implement for them.

这篇关于Ruby Geocoder如何设置最小近距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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