用于查找邮政编码的 Ruby Geocoder gem [英] Ruby Geocoder gem to find postal code

查看:55
本文介绍了用于查找邮政编码的 Ruby Geocoder gem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ruby​​ geocoder gem 按位置搜索.我想将搜索限制在已正式启动的位置.如果有人通过 zip 搜索,使用正则表达式很容易.但是如果有人按城市搜索,我需要将城市转换为邮政编码,检查我的邮政编码表,如果是肯定的,则返回搜索结果.

I'm using the ruby geocoder gem to search by location. I want to limit searches to locations that have officially launched. If someone searches by zip, this is easy using a regex. But if someone searches by city, I need to convert the city into a zip code, check my zip table and if positive, return the search results.

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

The geocoder api has a section for reverse geocoding:

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') 似乎返回了我需要的东西,我只是不知道如何得到它.这是我所在的位置:

This is obviously built just for using in the model. But I'm attempting to use it within my controller, but obj.zipcode is not working much at all. Geocoder.search('san jose, ca') seems to return what i need, i just don't know how to get to it. Here's where I'm at:

  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], 25, :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

推荐答案

试试区域宝石.你可以这样做:

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

这篇关于用于查找邮政编码的 Ruby Geocoder gem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆