2011-08-17 62 views

回答

2

这是因为这样做非常简单:

acts_as_gmappable :callback => :save_country 

    def save_country(data) 
    #what you need to do here 
    end 

data将包含来自谷歌的完整的哈希值。在你的榜样,它看起来像:

address_components:

  • LONG_NAME: “9” 类型:
    • street_number SHORT_NAME: “9”
  • LONG_NAME:街杜米雷类型:
    • route name:Rue du Muret
  • LONG_NAME:莱斯Amavaux类型:
    • 附近
    • 政治SHORT_NAME:莱斯Amavaux
  • LONG_NAME:14E郡类型:
    • sublocality
    • 政治SHORT_NAME:14E郡
  • LONG_NAME:马赛类型:
    • 地方
    • 政治SHORT_NAME:马赛
  • LONG_NAME:罗讷河口省类型:
    • administrative_area_level_2
    • 政治SHORT_NAME: “13”
  • long_name:“Provence-Alpes-C \ xC3 \ xB4te d'Azur”types:
    • administrative_area_level_1
    • 政治SHORT_NAME:PACA
  • LONG_NAME:法国类型:
    • 国家
    • 政治SHORT_NAME:FR
  • LONG_NAME: “13014” 类型:
    • POSTAL_CODE SHORT_NAME: “13014” 类型:
  • STREET_ADDRESS几何:位置: LNG:5.3805084 纬度:43.3315919范围: 东北: LNG:5.3805246 纬度:43.3315919 西南: LNG:5.3805084 LAT :43.331585 LOCATION_TYPE:RANGE_INTERPOLATED视: 东北: LNG:5.3818654802915 纬度:43.3329374302915 西南: LNG:5.3791675197085 纬度:43。3302394697085周的formatted_address:9街杜米雷,13014 马赛,法国
2

我想,如果我没有理解问题,这可能是你正在寻找 与已知的地址给定的模型正确的解决方案,自动获取地址部件和存储在单独的属性:

geocoded_by :address 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 :geocode 

每个地址解析器::结果对象,因此,提供以下数据:

result.latitude - float 
result.longitude - float 
result.coordinates - array of the above two 
result.address - string 
result.city - string 
result.state - string 
result.state_code - string 
result.postal_code - string 
result.country - string 
result.country_code - string 

如果您熟悉您使用的地理编码服务返回的结果,则可以访问更多数据,但您需要熟悉所使用的特定Geocoder :: Result对象以及结构您的地理编码服务的回应。

Thx