Geocoding multiple addresses in one model

前端 未结 2 789
余生分开走
余生分开走 2021-01-16 08:06

I am trying to geocode 2 addresses in a model using geocoder and I can\'t get gem to work as I want to. Here is the code that I am applying to my model:

clas         


        
2条回答
  •  青春惊慌失措
    2021-01-16 08:21

    The problem and solution are mentioned here.

    Add the following before_save and corresponding method to your model to solve the problem. Don't forget to repeat the part of code for the second location (maybe destination):

    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
    

提交回复
热议问题