使用Google-Maps-For-Rails更新模型地址的坐标

Enr*_*bas 3 google-maps ruby-on-rails ruby-on-rails-3 gmaps4rails

我已经注意到Google Maps For Rails宝石,否则它可以完美地工作,当我在模型字段中更改地址时,坐标不会自动更新,即使地址字段已更新并保存.我创建了一个调用geocode的before_save方法.

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
Run Code Online (Sandbox Code Playgroud)

这有效,但我想知道是否有必要,因为它似乎应该是宝石中的自动.我错过了什么吗?

谢谢...

PS地理编码返回一个数组,所以我只是采取了第一个(最佳)猜测

apn*_*ing 8

由于该gmaps4rails_address方法,刷新坐标是一个棘手的过程.它的灵活性如此易于使用,但对应的是不可能知道地址是否真的改变了.

这就是为什么我给编码员两个不同的机会来满足他们的需求:

  1. 如果您不关心性能,则可以在每次保存模型时刷新坐标(即使地址未更改).请参阅此处:https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Model-Customization.

更改:check_processfalse:

    acts_as_gmappable :check_process => false
Run Code Online (Sandbox Code Playgroud)

2.如果要完全控制过程,请在需要更新坐标时将gmaps布尔值设置为false.它可能是表单中的隐藏字段,也可能是模型中用于检查必要字段的钩子.