Using Geocoder, is there a way to save out the street name, city and zip to seperate columns?
Asked Answered
S

1

5

I'm in the process of switching my app to use geocoder. In my places table I have columns for address, lat, lng, street_address, city & zip. Using geocoder I'm happily able to fill lat, lng & address columns after validation with with the following in my places model

attr_accessible :address, :lat, :lng    
geocoded_by :address, :latitude  => :lat, :longitude => :lng
after_validation :geocode, :if => :address_changed? 

Is there a way to also have geocoder add the street name, city and zip to three other, separate columns?

Slide answered 24/1, 2012 at 16:8 Comment(0)
S
10

I'm still newish to rails so I missed this at first, but hope this helps someone else.

in my model

geocoded_by :address  do |obj,results|
  if geo = results.first
    obj.city    = geo.city
    obj.lat = geo.latitude
    obj.lng = geo.longitude
    obj.zip = geo.postal_code
    obj.state = geo.state
    obj.country = geo.country_code
  end
end

and in my view

 @tonic.address = params[:address]
Slide answered 25/1, 2012 at 3:12 Comment(1)
Thanks! I didn't know that geocoded_by could also take a blockOpium

© 2022 - 2024 — McMap. All rights reserved.