首页 > 解决方案 > 为 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

标签: ruby-on-railsgoogle-geocodergeocoderails-geocoder

解决方案


经过进一步的搜索和配置,我终于弄清楚了如何在 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

推荐阅读