Using rails gem geokit sort by distance and pagination?

I am facing a small problem in my application. I am currently using geokit to search for objects near a given location, and I am using sort_by_distance_from on the found set.

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 a way with geokit to paginate using DB when sorting by distance?

AKA without causing a complete set found?

+4
source share
2 answers

The distance column no longer works:

"In the current version of geokit-rails, it is not possible to add a where clause using the distance column. I tried many different ways to do this and didn't get it working."

It behaves the same for where and order clauses.

One could build a query like this:

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

This is not possible now, it must be done in one step:

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

github.com/geokit/geokit-rails

+1
source

My approach to solving this issue will use: offset and: limit parameters for find ()

there is also a distance field for geokit models : 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) 
0
source

All Articles