为 Geocoder on Rails 使用多个 api 键

问题描述

我正在使用 Rails Geocoder gem 对用户提交的街道地址的纬度和经度进行地理编码。我还想自动拉取用户的 IP 位置,以在主屏幕上填充其他用户提交的位置附近的地址。

我想使用 IPinfo 的 API 密钥来获取用户的位置,并使用不同的 API 密钥让 Google 对街道地址的纬度/经度进行地理编码。以前,当我调用两个 api 键时,我收到一条警告消息,指出第一个键被第二个覆盖,可能是因为它们具有相同的名称

我在上一篇文章中看到一个用户回答说他们可以通过 passing the service as a lookup value 来完成,但是他们是如何在 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

解决方法

经过进一步的搜索和配置后,我终于找到了如何在 Geocoder on Rails 中使用多个 API 密钥。回答我自己的问题,以防其他人遇到同样的问题。

Rails Geocoder 文档确实告诉了您如何use multiple apis,但没有告诉您如何为不同的服务进行配置(至少对于像我这样的菜鸟)。我最终不得不将服务的特定符号传递给我希望服务使用的选项,例如:

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,# 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...