1

这个网址有一个演示http://gmaps4rails.heroku.com/locations

当我们提供详细地址时,例如“9 Rue du Muret, Marseille”,所以在谷歌地图中它会显示一个点,

现在我想通过地址获取特定城市“马赛”不包含街道,只有城市名称,

有什么方法可以实现吗?

4

2 回答 2

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

每个 Geocoder::Result 对象 result 提供以下数据:

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 对象以及您的地理编码的结构服务的响应。

谢谢

于 2011-08-18T19:20:47.250 回答
2

就像这样做一样简单:

  acts_as_gmappable :callback => :save_country 

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

data将包含来自谷歌的完整哈希。在您的示例中,它看起来像:

地址组件:

  • long_name:“9”类型:
    • 街道号码短名称:“9”
  • long_name:Rue du Muret 类型:
    • 路线短名称:Rue du Muret
  • long_name:Les Amavaux 类型:
    • 邻里
    • 政治简称:Les Amavaux
  • long_name:14e 区类型:
    • 次地方性
    • 政治简称:14e 区
  • long_name:马赛类型:
    • 地方性
    • 政治简称:马赛
  • long_name:Bouches-du-Rhone 类型:
    • 行政区域级别_2
    • 政治短名:“13”
  • long_name:“Provence-Alpes-C\xC3\xB4te d'Azur”类型:
    • 行政区域级别_1
    • 政治简称:PACA
  • long_name:法国类型:
    • 国家
    • 政治简称:FR
  • long_name:“13014”类型:
    • 邮政编码短名称:“13014”类型:
  • street_address geometry: location: lng: 5.3805084 lat: 43.3315919 bounds: northeast: lng: 5.3805246 lat: 43.3315919 southwest: lng: 5.3805084 lat: 43.331585 location_type: RANGE_INTERPOLATED viewport: northeast: lng: 5.3818654802915 lat: 43.3329374302915 southwest: lng: 5.3791675197085 lat: 43.3302394697085 formatted_address: 9 Rue du Muret, 13014 Marseilles, France
于 2011-08-18T08:18:46.823 回答