4

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.

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?

AKA, not calling the full found set?

Nick Faraday
  • 598
  • 5
  • 23
  • 1
    I too am looking for a solution for this. Combining with will_paginate creates incorrect results. – benr75 Oct 05 '10 at 00:08

2 Answers2

1

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."

It behaves the same way for where and order clauses.

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

mauriciomdea
  • 2,954
  • 3
  • 15
  • 10
0

My approach to solve this, would use the :offset and :limit parameters for the find()

also, there is 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)
x0rist
  • 625
  • 4
  • 9
  • 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." It behaves the same way for where and order clauses. https://github.com/geokit/geokit-rails – mauriciomdea Oct 20 '15 at 15:44