I am trying to use geocoder gem but confused while using reverse geocoding.
I have "region" model with fields- country, state, city and zipcode.
If user fill only zipcode then i want to fill all other field automatically.
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
but unable to understand what obj and result is and how to set obj and result.
please help me by giving example.
i have written a blog explaining how it works.you need to geocode and get the relevant informations from geocoder
gem.
User will enter address using Geocomplete and store that address in address column in users/locations table Then,use Geocoder to fetch other geo informations and update other columns using address.
Here it goes................
================users/locations table,here i will use the allow the user to fill in the address using Geocomplete and then use it to get other details using Geocoder
class CreateLocations < ActiveRecord::Migration
def change
create_table :places do |t|
t.string :address
t.float :latitude
t.float :longitude
##======here the address field is important==========
t.string :address
t.string :country
t.string :state
t.string :city
t.string :pincode
t.timestamps
end
add_index :places, :address
end
end
==================Geocode to autopopulate using address in users/location model
##i want to use the address column to autopopulate others columns
geocoded_by :address
##also i want to use the latitude.longitude to fetch all others informations and then save in relevant ##feilds
reverse_geocoded_by :latitude, :longitude do |obj,results|
if geo = results.first
obj.state = geo.state
obj.city = geo.city
obj.pincode = geo.postal_code
obj.country = geo.country
end
end
##change/update/validate address only if address changed to improved performance else every time it ##will keep updating
after_validation :geocode, :reverse_geocode ,:if => :address_changed?