【问题标题】:rails3 how to get specific city from detail address in google maprails3如何从谷歌地图中的详细地址获取特定城市
【发布时间】:2011-08-17 16:51:28
【问题描述】:

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

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

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

有什么方法可以实现吗?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 google-maps gmaps4rails


    【解决方案1】:

    就这么简单:

      acts_as_gmappable :callback => :save_country 
    
      def save_country(data)
        #what you need  to do here
      end
    

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

    地址组件:

    • long_name:“9”类型:
      • street_number short_name: "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 类型:
      • administrative_area_level_2
      • 政治短名:“13”
    • long_name:“Provence-Alpes-C\xC3\xB4te d'Azur”类型:
      • administrative_area_level_1
      • 政治简称:PACA
    • long_name:法国类型:
      • 国家
      • 政治简称:FR
    • long_name:“13014”类型:
      • 邮政编码短名称:“13014”类型:
    • street_address 几何:位置: 液化天然气:5.3805084 纬度:43.3315919 界限: 东北: 液化天然气:5.3805246 纬度:43.3315919 西南: 液化天然气:5.3805084 纬度:43.331585 location_type:RANGE_INTERPOLATED 视口: 东北: 液化天然气:5.3818654802915 纬度:43.3329374302915 西南: 液化天然气:5.3791675197085 纬度:43.3302394697085 格式化地址:9 Rue du Muret, 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
      

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

      谢谢

      【讨论】:

        猜你喜欢
        • 2023-03-20
        • 2010-11-09
        • 2020-05-21
        • 1970-01-01
        • 2016-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多