Rails从ZIP Geoip获取状态

时间:2015-06-21 02:39:29

标签: ruby-on-rails ruby geoip

我有他们输入的用户的邮政编码,并希望预先填写城市/州的几个字段。有谁知道获取这些信息的好方法?我想做这样的事情:

GeoIP.new('geoip/GeoLiteCity.dat').zip('53593')

我知道我可以做GeoIP.new('geoip/GeoLiteCity.dat').city(request.remote_ip),但那是他们当时所处的地方,而不是他们实际居住的地方。

1 个答案:

答案 0 :(得分:3)

我不认为geoip Ruby gem(Github repo)能够通过邮政编码进行搜索。扫描the geoip.rb source时,zippostal_code没有方法。

您可以使用另一个宝石my_zipcode_gem。这也是hosted on Github,并具有以下语法:

zipcode = Zipcode.find_by_code '66206'
zipcode.state.abbr    # => 'KS'
zipcode.city          # => 'Shawnee Mission'
zipcode.county.name   # => 'Johnson'
zipcode.lat.to_s      # => '38.959356', it is actually a BigDecimal object converted to_s for documentation.
zipcode.lon.to_s      # => '-94.716155', ditto
zipcode.is_geocoded?  # => true, most if not all should be pre-geocoded.