加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 用于查找邮政编码的Ruby Geocoder gem

发布时间:2020-12-17 03:02:01 所属栏目:百科 来源:网络整理
导读:我正在使用 ruby geocoder gem按位置搜索.我想将搜索限制在已正式启动的位置.如果有人通过zip搜索,使用正则表达式很容易.但如果有人按城市搜索,我需要将城市转换成邮政编码,检查我的邮政编码表,如果是肯定的,则返回搜索结果. geocoder api有一个反向地理编码
我正在使用 ruby geocoder gem按位置搜索.我想将搜索限制在已正式启动的位置.如果有人通过zip搜索,使用正则表达式很容易.但如果有人按城市搜索,我需要将城市转换成邮政编码,检查我的邮政编码表,如果是肯定的,则返回搜索结果.

geocoder api有一个反向地理编码部分:

reverse_geocoded_by :latitude,:longitude do |obj,results|
  if geo = results.first
    obj.city    = geo.city
    obj.zipcode = geo.postal_code
    obj.country = geo.country_code
  end
end
after_validation :reverse_geocode

这显然是为了在模型中使用而构建的.但我试图在我的控制器中使用它,但是obj.zipcode根本不起作用. Geocoder.search(‘san jose,ca’)似乎回归了我需要的东西,我只是不知道如何去做.这是我在的地方:

if params[:search].present?
#       coords = Geocoder.coordinates(params[:search])
    zip = Geocoder.search(params[:search])
    if Zip.where(:zip => (params[zip.zipcode]),:launch => true).exists?
      addresses = Address.near(params[:search],25,:order => :distance)
      @profiles = addresses.map{ |ad| ad.profile }.uniq unless addresses.nil?
      @title = "Addresses Near " + (params[:search]).to_s
    else
      if Zip.where(:zip => (params[zip.zipcode]),:nearlaunch => true).exists?
        addresses = Address.near(params[:search],:order => :distance)
        @profiles = addresses.map{ |ad| ad.profile }.uniq unless addresses.nil?
        @title = "We have not launched for your location but these are near by " + (params[:search]).to_s
      else
        redirect_to :controller => 'pages',:action => 'notlaunched'
      end
    end

解决方法

试试 Area gem.你可以这样做:

"San Jose,CA".to_zip #=> ["95101","95102",...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读