2015-03-31 57 views
0

我想使用geocoder gem,但使用反向地理编码时感到困惑。什么是地理编码对象和地理编码器中的地理编码::结果对象的数组gemoder gem

我有“地区”模型与字段国家,州,城市和邮编。

如果用户只填写邮编,那么我想自动填写所有其他字段。

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和结果是什么以及如何设置obj和结果。

请举例来帮助我。

回答

1

我写了一个blog解释它是如何工作的。您需要进行地理编码并从geocoder gem获取相关信息。

用户将使用Geocomplete输入地址并将该地址存储在用户/位置表中的地址列中 然后,使用Geocoder获取其他地理信息并使用地址更新其他列。

这里去................

================用户/地点表,在这里我将使用允许用户使用Geocomplete填写地址,然后用它利用地理编码

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 

得到其他细节================= =在用户/位置模型中使用地址自动填充的地理编码

##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 

##变/更新/验证地址只有地址变更为其他性能改进每次##时间会不断更新

after_validation :geocode, :reverse_geocode ,:if => :address_changed? 
+0

感谢,这正是我需要的 – Adt 2015-03-31 13:10:39