Ruby-on-rails – Update the coordinates of the modified model address using Google-Maps-for-Rails

I have noticed the Google Maps For Rails gem, otherwise it can work perfectly, when I change the address in the model field, the coordinates will not be updated automatically, even if the address field is updated and Save. I created a before_save method that calls geocode.

before_save :update_location_coordinates # in model being mapped

protected

def update_location_coordinates
place = Gmaps4rails.geocode(gmaps4rails_address).first
self.longitude, self.latitude = place[:lng], place[:lat] unless place.empty?
rescue
nil
end

This works, but I want to know if it is necessary because it seems to be automatic in gems. Am I missing something?

Thanks…

PS geocoding returns an array, so I just took the first (best) guess

Because of the gmaps4rails_address method, refreshing the coordinates is a tricky process. Its flexibility is so easy to use, but it is impossible to know whether the address has actually changed.

This is why I give coders two different opportunities to meet their needs:

>If you don’t care about performance, you can save the model every time Refresh the coordinates (and then even if the address has not changed). See: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Model-Customization.

Change: check_process is false:< /p>

acts_as_gmappable :check_process => false

2. If you want to fully control the process, please set the gmaps boolean to false when you need to update the coordinates. It It may be a hidden field in the form, or it may be a hook in the model to check the necessary fields.

I have noticed the Google Maps For Rails gem, otherwise it can be perfect When I change the address in the model field, the coordinates will not be updated automatically, even if the address field is updated and saved. I created a before_save method that calls geocode.

before_save :update_location_coordinates # in model being mapped

protected

def update_location_coordinates
place = Gmaps4rails.geocode(gmaps4rails_address).first
self .longitude, self.latitude = place[:lng], place[:lat] unless place.empty?
rescue
nil
en d

This works, but I want to know if it is necessary because it seems to be automatic in gems. Am I missing something?

Thanks…

PS geocoding returns an array, so I just took the first (best) guess

Because of the gmaps4rails_address method, refreshing the coordinates is a tricky process. Its flexibility is so easy to use, but it is impossible to know whether the address has really changed.

This is Why do I give coders two different opportunities to meet their needs:

>If you don’t care about performance, you can refresh the coordinates every time you save the model (and then even if the address has not changed). See: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Model-Customization.

Change: check_process is false:

acts_as_gmappable :check_process => false

2. If you want to fully control the process, please set the gmaps boolean value to false when you need to update the coordinates. It may be a hidden field in the form or a model The hook used to check the necessary fields.

Leave a Comment

Your email address will not be published.