Rails Get State from ZIP Geoip

1.7k views Asked by At

I have the zip code of the user that they entered and would like to pre-populate a few fields with the city/state. Does anyone know of a good way to get this information? I want to do something like this:

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

I know I could do GeoIP.new('geoip/GeoLiteCity.dat').city(request.remote_ip) but that's where they are at the time as opposed to where they may actually live.

1

There are 1 answers

2
Grokify On BEST ANSWER

I don't think the geoip Ruby gem (Github repo) has the ability to search by zip / postal code. When scanning the geoip.rb source, there is no method for zip or postal_code.

There is another gem, my_zipcode_gem, that you may be able to use. This is also hosted on Github and has the following syntax:

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.