Rails 查找附近附近坐标,除了 postgis 还有啥好方案?

hunkguo · July 02, 2017 · Last by hunkguo replied at July 03, 2017 · 1722 hits

postgis 在查询时有点 Hold 不住,st_DWithin 等函数总是说找不到,然后是坐标字段,有的用 point,有的用 geometry,而 geometry 似乎在 rails 又不能识别。 总之错误连连。

只是想实现查找附近坐标功能,postgis 按理说最省事,结果也是一堆问题,有最近成功的朋友吗? 难道要换 mongodb?

redis 也可以

有时候挖坑也是好的

elasticsearch ?

postgis 哪里有问题…

Reply to hooopo

调用 st 开头的几个函数时,总提示错误,估计是参数格式不对,但搜到的资料,用法各不相同。不知以哪里的为准。

Reply to hunkguo

自己没搞明白,不等于 postgis 有问题。你这样换其它方案也不见得搞的通…

Reply to hooopo

是没找到正确的方法,没少搜索,没找到解决的线索。

@hunkguo 其实单独这个功能,根本不需要使用 postgis,给你推荐一个 Gem

# Gemfile
gem 'activerecord-postgres-earthdistance'

运行:

$ bundle install
$ rails g earth_distance:setup

在 model 里面添加一个:

class Place < ActiveRecord::Base
  acts_as_geolocated
end

使用的时候:

Place.within_radius(100, -22.951916, -43.210487).all

如果不想用 gem, 也可以自己为 postgresql 安装 earth 插件,直接用 sql 语句也可以。

Reply to rocLv

谢谢,这个 gem 之前没搜到,研究下,谢谢!

You need to Sign in before reply, if you don't have an account, please Sign up first.