【问题标题】:Use multiple api keys for Geocoder on Rails为 Geocoder on Rails 使用多个 api 键
【发布时间】:2021-07-08 05:40:21
【问题描述】:

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

我想为 IPinfo 使用 API 密钥来获取用户的位置,并为 Google 使用不同的 API 密钥来对街道地址的纬度/经度进行地理编码。以前,当我调用两个 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

【问题讨论】:

  • 您如何评论其他问题(或答案)以获取更多详细信息?
  • @MrUpsidown 哈哈我试图向提供答案的人发表评论,但我缺乏最低声誉分数来发表评论。你需要 15 个,而我只有 13 个。

标签: ruby-on-rails google-geocoder geocode rails-geocoder


【解决方案1】:

经过进一步的搜索和配置,我终于弄清楚了如何在 Rails 上的 Geocoder 中使用多个 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,         # 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多