简体   繁体   English

Geocoder Gem .near可以工作超过1000

[英]Geocoder Gem .near working for distances over 1000

I am using the Geocoder Gem. 我正在使用Geocoder宝石。 When I use .near I get no results unless I set the distance to 500 to 1000. 当我使用.near除非将距离设置为500到1000,否则不会有任何结果。

I created this query: 我创建了这个查询:

User.near([53.2910642, -6.1969235], 20).any?

But do not get any results, but: 但是没有得到任何结果,但是:

User.near([53.2910642, -6.1969235], 2000).any?

Does. 有。 I have a user in the database. 我在数据库中有一个用户。

I have a user in the database with a latitude of 53.2910642 and longitude of -6.1969235. 我在数据库中有一个用户,其纬度为53.2910642,经度为-6.1969235。 These are stored in Float columns and in the User model I have: 这些存储在Float列和用户模型中,我具有:

#Geocoding
geocoded_by :address
after_validation :geocode

Is their something obvious that I am missing? 他们明显我想不起来吗? I am testing this in the test environment, in the Rails Console. 我正在Rails控制台的测试环境中对此进行测试。

Depending on the database backend you're using, floating-point precision might be different than what you expect. 根据您使用的数据库后端,浮点精度可能与您期望的有所不同。

Eg you might want to check that you're at least using a precision of 15 and a scale of 10. A rails column migration would be created using: 例如,您可能想要检查您是否至少使用15的精度和10的比例。将使用以下方法创建rails列迁移:

[ :longitude, :latitude ].each do |c|
  t.decimal c, precision: 15, scale: 10
  t.decimal c, precision: 15, scale: 10
end

Also, if you're planning to query your database for entries based on their location, make sure you're using correct index for those columns! 另外,如果您打算根据位置查询数据库中的条目,请确保对这些列使用正确的索引!

The queries that are issued to a database backend for querying a distance between two coordinates could be heavy. 发出到数据库后端以查询两个坐标之间的距离的查询可能很繁琐。 When I worked on similar projects, I ended up rewriting this kind of functionality to allow my models to store intermediate values. 当我从事类似的项目时,我最终重写了这种功能,以允许我的模型存储中间值。

For instance, if your distance computation formula looks like the one described here: http://www.movable-type.co.uk/scripts/latlong.html , you might want to create indexed columns to store the result of φ1 and φ2, as well as columns to store results of the cosine / sine of φ1 and φ2. 例如,如果您的距离计算公式看起来像这里描述的公式: http : //www.movable-type.co.uk/scripts/latlong.html ,则可能需要创建索引列来存储φ1和φ2的结果。以及用于存储φ1和φ2的余弦/正弦结果的列。

This way, you will be able to optimize the database queries and greatly improve run times. 这样,您将能够优化数据库查询并大大缩短运行时间。

Another approach I ended up using was to do a rough bounding-box based SQL query based on the radius, returning a list of candidates for a more complex geodesic based formula. 我最终使用的另一种方法是基于半径进行基于边界框的粗略SQL查询,返回更复杂的基于测地线的公式的候选列表。 This way, the geodesic SQL formula could either pre-select candidates using the IN [...] operator, or even completely be by-passed and be done with your code using the candidates array. 这样,测地线的SQL公式既可以使用IN [...]运算符预先选择候选者,也可以完全绕过并使用候选人数组对代码进行处理。

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

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