开发者

rails geocoder near scope

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

Obj.ne开发者_如何学Car(@address, 10)

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


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) }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜