Ruby Geocoder gem to find a zip code

I use ruby ​​geocoder to search by location. I want to limit the search to sites that were officially launched. If someone is looking for zip, this is easy to use with regex. But if someone is looking for a city, I need to convert the city to a zip code, check my mail table and, if positive, return the search results.

api geocoder 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 

This is obviously only built for use in the model. But I'm trying to use it in your controller, but obj.zipcode not work at all. Geocoder.search ('san jose, ca') seems to return what I need, I just don't know how to get to it. Here where I am:

  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 params [: search]). to_s  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 ,: action => 'notlaunched'  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 
+4
source share
2 answers

Try Cloud pearls . You can do something like:

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

You can use this to get zipcode for any latitude and longitude.

 Geocoder.search([latitude, longitude]).first.postal_code 

This can be used in the controller or any other files except models. But Geocoder also provides automatic conversion, in which the table must have the required column names. For more information, visit here geocoder

+1
source

Source: https://habr.com/ru/post/1391955/


All Articles