Petya petrov
Petya petrov

Reputation: 2213

rails geocoder near scope

Is there a way to get, for example, ten nearest places, no matter how big distance from object is?

Obj.near(@address, 10)

And this scope should return 10 nearest places, not objects within radius 10.

Upvotes: 1

Views: 829

Answers (1)

Chris Bailey
Chris Bailey

Reputation: 4136

I'd suggest taking a look at GeoRuby and spatial_adapter. Unless you want to load a lot of data from your database and then doing the distance comparison yourself I'd suggest that you use PostgreSQL as your DB engine, as it has great Geo/GIS support. Putting the three together means that assuming you can pass a GeoRuby object as your @address, a scope approximatly like the following would do the job you need

scope :near lambda { |location, count| order("ST_Distance(geom_field_in_table, ST_GeomFromWKB(#{location.as_wkb}, -1))").limit(count) }

Upvotes: 1

Related Questions