【问题标题】:Geocoding two addresses in one model在一个模型中对两个地址进行地理编码
【发布时间】:2014-10-23 00:41:37
【问题描述】:

我想在一个模型中对两个地址进行地理编码,即。 Journey 有一个 StartAddress 和 EndAddress。 因此, 我的模型有三个属性: 起始地址 start_longtitude 开始纬度 结束地址 end_longtitude end_lattitude

我如何对其进行地理编码,以便我可以调用 nears 函数来查找 i) 在给定旅程附近开始的旅程和 ii) 在特定旅程附近结束的旅程? 任何帮助将不胜感激

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 geocoding rails-geocoder


    【解决方案1】:

    最近的更新允许这样做。 https://github.com/alexreisner/geocoder/pull/692

    使用您自己的地理编码函数对模型进行地理编码,然后在 near 函数中指定您要搜索的替代属性。

    【讨论】:

      【解决方案2】:

      问题和解决方案here表达的很好。

      为您的模型添加以下before_save 和相应的方法以解决问题。不要忘记为第二个位置(可能是目的地)重复部分代码:

      before_save :geocode_endpoints
      
        private
        #To enable Geocoder to works with multiple locations
        def geocode_endpoints
          if from_changed?
            geocoded = Geocoder.search(loc1).first
            if geocoded
              self.latitude = geocoded.latitude
              self.longitude = geocoded.longitude
            end
          end
          # Repeat for destination
              if to_changed?
            geocoded = Geocoder.search(loc2).first
            if geocoded
              self.latitude2 = geocoded.latitude
              self.longitude2 = geocoded.longitude
            end
          end
        end
      

      【讨论】:

        猜你喜欢
        • 2015-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-07
        • 1970-01-01
        相关资源
        最近更新 更多