Best Zip Code Plugin for Ruby [closed]

♀尐吖头ヾ 提交于 2019-11-30 12:07:27

问题


I need to find the city and state from a zip code. Does anyone know a good plugin/API that I can use to do this?


回答1:


gem install geokit

In IRB:

require 'geokit'
geo = GeoKit::Geocoders::MultiGeocoder.multi_geocoder('90210')
if geo.success
  geo.state # => CA
  geo.city  # => Beverly Hills
end



回答2:


A more lightweight option is the Area gem.

require 'area'

'11211'.to_region #=> "Brooklyn, NY"



回答3:


See Jason's answer. It works nicely.


The problem is that the USPS doesn't allow bulk downloads of their zip-code lists unless you pay for it. Google's API, which is used in the gem mentioned by Splashlin, no longer seems to support the city and state, instead it now returns the area code:

require 'open-uri'
require 'json'

json = JSON::parse(open('http://maps.google.com/maps/geo?q=852581').read)
puts json
# >> {"name"=>"852581", "Status"=>{"code"=>602, "request"=>"geocode"}}

This page shows some ways you could roll your own. The sources of the data might not be current though:

http://www.ruby-forum.com/topic/48815



来源:https://stackoverflow.com/questions/3935062/best-zip-code-plugin-for-ruby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!