在我的模型中输入像这样的坐标时,我试图得到地址。
def full_address
Geocoder.address("#{self.latlon.x}, #{self.latlon.y}")
end然而,有时它的工作非常好,然后有时我会得到这个错误。我到处寻找解决办法,但没有一个适用于我。
warning: constant Geokit::Geocoders::Geocoder::TimeoutError is deprecated
E, [2016-11-15T17:06:32.828481 #7406] ERROR -- : Caught an error during Google geocoding call: SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A
E, [2016-11-15T17:06:32.828665 #7406] ERROR -- : /Users/Brandon/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/net/http.rb:933:in `connect_nonblock'发布于 2016-12-22 18:34:56
我不打算发表评论,但没有人发布任何信息。由于您的特定错误消息包含SSL警告,我将检查您是否需要显式地要求openssl和/或openssl设置是否正确。在我的应用程序中,地理编码超时也遇到了类似的问题,尽管堆栈跟踪有点不同。需要很长时间才能完成Geocoding (例如,因为地址非常奇怪),但我的其余代码仍在继续。我认为堆栈跟踪是Geokit中的一个bug。由于您无法控制这种运行的速度,所以首先应该考虑做一些事情来减少发出此调用的次数,因此,至少要做到以下几点:
def full_address
@full_address ||= Geocoder.address("#{self.latlon.x}, #{self.latlon.y}")
end您还应该考虑将full_address存储为计算字段,因为每次第一次访问该模型实例时,上述代码仍将运行。因此,去掉上面的代码,添加一个full_address字段和一个预保存钩子:
在迁移文件中:
add_column :models, :full_address, :string在你的model.rb中:
before_save :geocode_address
def geocode_address
self.full_address = Geocoder.address("#{self.latlon.x}, #{self.latlon.y}") if self.changed.include?("latlon")
end这样,调用只在更新latlon.x和latlon.y变量时运行,而不是每次访问地址时都运行。
https://stackoverflow.com/questions/40620642
复制相似问题