ruby geocoder gem を使用して場所で検索しています。検索を正式に開始した場所に限定したい。誰かが zip で検索する場合、正規表現を使用すると簡単です。しかし、誰かが市区町村で検索した場合、市区町村を郵便番号に変換し、郵便番号表をチェックして、正しければ検索結果を返す必要があります。
ジオコーダー APIには、リバース ジオコーディング用のセクションがあります。
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') は必要なものを返すようですが、その方法がわかりません。私がいる場所は次のとおりです。
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