ruby-on-rails - 为 Geocoder on Rails 使用多个 api 键
问题描述
我正在使用 Rails Geocoder gem 对用户提交的街道地址的纬度和经度进行地理编码。我还想自动提取用户 IP 位置,以在主屏幕上填充其他用户在其位置附近提交的地址。
我想使用 IPinfo 的 API 密钥来获取用户的位置,并使用不同的 API 密钥让 Google 对街道地址的纬度/经度进行地理编码。以前,当我调用两个 api 键时,我会收到一条警告消息,指出第一个键被第二个键覆盖,可能是因为它们具有相同的名称。
我看到上一篇文章,用户回答说他们可以通过将服务作为查找值传递来做到这一点,但是他们是如何在 geocoder.rb 配置文件中设置 api 键的?他们是否更改了第二个服务的 api 名称?他们是否必须添加另一个配置文件?
任何帮助将不胜感激。我当前的配置文件如下。我现在注释掉了 IPinfo 查找,所以我仍然可以对生产中的项目进行地理编码。谢谢你。
if Rails.env.production?
Geocoder.configure(
# Geocoding options
timeout: 5, # geocoding service timeout (secs)
lookup: :google, # name of geocoding service (symbol)
# ip_lookup: :ipinfo_io, # name of IP address geocoding service (symbol)
# api_key: ENV['IPINFO_IO_API_KEY'], #API key for ip lookup service
# language: :en, # ISO-639 language code
use_https: true, # use HTTPS for lookup requests? (if supported)
# http_proxy: nil, # HTTP proxy server (user:pass@host:port)
# https_proxy: nil, # HTTPS proxy server (user:pass@host:port)
api_key: ENV['GOOGLE_GEOCODER_API_KEY'], # API key for geocoding service
# cache: Redis.new, # cache object (must respond to #[], #[]=, and #del)
# cache_prefix: 'geocoder:', # prefix (string) to use for all cache keys
# Exceptions that should not be rescued by default
# (if you want to implement custom error handling);
# supports SocketError and Timeout::Error
# always_raise: [],
# Calculation options
# units: :mi, # :km for kilometers or :mi for miles
# distances: :linear # :spherical or :linear
)
end
解决方案
经过进一步的搜索和配置,我终于弄清楚了如何在 Rails 上的 Geocoder 中使用多个 API 密钥。回答我自己的问题,以防其他人有同样的问题。
Rails Geocoder 文档确实告诉您如何使用多个 api,但不确切地告诉您如何为不同的服务进行配置(至少对于像我这样的菜鸟而言)。我最终不得不将服务的特定符号传递给我希望服务使用的选项,例如:
lookup: :google, # name of geocoding service (symbol)
google: {
api_key: ENV['GOOGLE_GEOCODER_API_KEY'], # API key for geocoding service
}
请参阅下面的完整代码。
if Rails.env.production?
Geocoder.configure(
# Geocoding options
timeout: 5, # geocoding service timeout (secs)
lookup: :google, # name of geocoding service (symbol)
google: {
api_key: ENV['GOOGLE_GEOCODER_API_KEY'], # API key for geocoding service
},
ip_lookup: :ipinfo_io, # name of IP address geocoding service (symbol)
ipinfo_io: {
api_key: ENV['IPINFO_IO_API_KEY'], #API key for ip lookup service
},
# language: :en, # ISO-639 language code
use_https: true, # use HTTPS for lookup requests? (if supported)
# http_proxy: nil, # HTTP proxy server (user:pass@host:port)
# https_proxy: nil, # HTTPS proxy server (user:pass@host:port)
# cache: Redis.new, # cache object (must respond to #[], #[]=, and #del)
# cache_prefix: 'geocoder:', # prefix (string) to use for all cache keys
# Exceptions that should not be rescued by default
# (if you want to implement custom error handling);
# supports SocketError and Timeout::Error
# always_raise: [],
# Calculation options
# units: :mi, # :km for kilometers or :mi for miles
# distances: :linear # :spherical or :linear
)
end
推荐阅读
- swift - 可选的委托方法不是可选的
- quill - QuillJS:自定义 attr alt
- node.js - 如何将 NodeJs 日志从控制台重定向到文件
- android - 在android中的click事件上执行耗时的任务
- jquery - Kendo UI Grid Multicheck 按 Id 过滤
- html - 如何使div中的背景图像响应
- hadoop - Hadoop集群处于安全模式(Namenode处于安全模式)我需要释放哪些资源才能删除安全模式?
- c# - Visual Studio 2017 中没有启动对象
- cordova - 将 StartApp 广告集成到 Ionic 应用程序中
- ruby-on-rails - 在 rspec after(:suite) 中访问测试结果