我只想将geokit
gem与以下俄语字符一起使用(从rails控制台进行测试):
Geokit::Geocoders::GoogleGeocoder.geocode 'минск'
但是得到下一个错误:
Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT
from /usr/share/ruby-rvm/gems/ruby-1.9.3-p125/gems/geokit-1.6.5/lib/geokit/geocoders.rb:464:in `do_geocode'
from /usr/share/ruby-rvm/gems/ruby-1.9.3-p125/gems/geokit-1.6.5/lib/geokit/geocoders.rb:129:in `geocode'
from (irb):16
from /usr/share/ruby-rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
from /usr/share/ruby-rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from /usr/share/ruby-rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
下一个可能的解决方案于事无补:
Geokit::Geocoders::GoogleGeocoder.geocode 'минск'.force_encoding('UTF-8')
Geokit::Geocoders::GoogleGeocoder.geocode(CGI.escape('Минск'))
#in config/environment.rb
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
什么可以帮助解决这个问题?我对google-places
宝石也有同样的问题。
这里的问题是记录器。
https://github.com/imajes/geokit-gem/blob/master/lib/geokit/services/google3.rb#L49
在这一行,它连接UTF-8 address
字符串和ASCII json
响应。修复方法是在这行之前添加以下代码:
json.force_encoding('utf-8') if json.respond_to?(:force_encoding)
您可以执行此操作并发送提取请求。刚刚对Nomatim地理编码器做了同样的操作。