简体   繁体   English

使用rails gem geokit按距离和分页排序?

[英]Using rails gem geokit sort by distance and pagination?

I come across a small issue in my app. 我在我的应用中遇到了一个小问题。 I'm currently using geokit to find objects near a given location, and I use the sort_by_distance_from on the found set. 我目前正在使用geokit查找给定位置附近的对象,并在找到的集合上使用sort_by_distance_from。

See below: 见下文:

@find = Item.find(:all, :origin =>[self.geocode.lat.to_f,self.geocode.lng.to_f], :within=>50, :include=>[:programs], :conditions=>["programs.name = ?", self.name])
  @find.sort_by_distance_from([self.geocode.lat.to_f,self.geocode.lng.to_f]

Is there any way with geokit to paginate form the DB when sorting by distance? 在按距离排序时,是否有任何方法可以使用geokit对数据库进行分页?

AKA, not calling the full found set? AKA,没有调用完整的发现集?

The distance column isn't working anymore: 距离列不再有效:

"In the current version of geokit-rails, it is not possible to add a where clause using the distance column. I've tried many different ways to do this and didn't get it working." “在当前版本的geokit-rails中,不可能使用距离列添加where子句。我已经尝试了很多不同的方法来做到这一点并且没有让它工作。”

It behaves the same way for where and order clauses. 它对于where和order子句的行为方式相同。

One would expect to build a query like this : 人们期望建立这样的查询:

scoped  = Location.geo_scope(:origin => @somewhere)
scoped  = scoped.where('distance <= 5')
results = scoped.all

This is not possible right now, it must be done in a single step like this: 这是不可能的,它必须在一个步骤中完成,如下所示:

scoped = Location.within(5, :origin => @somewhere)
results = scoped.all

github.com/geokit/geokit-rails github.com/geokit/geokit-rails

My approach to solve this, would use the :offset and :limit parameters for the find() 我解决这个问题的方法是使用find()的:offset和:limit参数

also, there is a distance field for geokit models, :order=>'distance asc' 此外,geokit模型还有一个距离场:order =>'distance asc'

eg. 例如。

page = 0 unless params[:page]
items_per_page = 20
offset = page * items_per_page
@find = Item.find(:all, :origin =>[self.geocode.lat.to_f,self.geocode.lng.to_f], :within=>50, :include=>[:programs], :conditions=>["programs.name = ?", self.name], :order => 'distance asc', :limit => items_per_page, :offset => page)

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

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