什么是要进行地理编码的对象以及地理编码器 gem 中的 Geocoder::Result 个对象数组

what is object to be geocoded and an array of Geocoder::Result objects in geocoder gem

我正在尝试使用地理编码器 gem 但在使用反向地理编码时感到困惑。

我有 "region" 模型,其中包含国家、州、城市和邮政编码字段。

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

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

请举例说明。

我写了一个 blog 解释它 works.you 如何需要地理编码并从 geocoder gem.

获取相关信息

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

开始了......

================users/locations table,这里我会使用允许用户填写地址使用 Geocomplete 然后使用它来使用 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

==================地理编码以使用 users/location 模型中的地址自动填充

##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 地址仅当地址更改以提高性能时,否则每次它 ## 将继续更新

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