【问题标题】:Does Geocoder gem work with google API key?Geocoder gem 是否与谷歌 API 密钥一起使用?
【发布时间】:2012-07-29 19:48:15
【问题描述】:

我正在为我的项目使用 ruby​​ geocoder gem,随着项目的发展,我开始考虑连接到 Google API 密钥。将此添加到项目后:

Geocoder.configure do |config|

# geocoding service (see below for supported options):
config.lookup = :google

# to use an API key:
config.api_key = 'my_key'

# geocoding service request timeout, in seconds (default 3):
config.timeout = 5

end 

我收到 Google 地理编码 API 错误:请求被拒绝。当我启动应用程序时。通过阅读,如果其他人选择继续使用 gem,似乎他们会切换到雅虎。我可以将 gem 配置为使用 google api 密钥吗?主要是我想留意每日查询的数量,以避免超出限制。

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3 geocoding rails-geocoder


【解决方案1】:

创建一个文件:config/initializers/geocoder.rb 并像这样设置:

Geocoder.configure(
  lookup: :google_premier,
  api_key: ['api_key', 'client_id', 'client_id_type'],
)

【讨论】:

    【解决方案2】:

    我今天遇到了这个问题,并通过设置 use_https 来解决它,例如

    Geocoder.configure(
      timeout: 15,
      api_key: "YOUR_KEY",
      use_https: true
    )
    

    【讨论】:

      【解决方案3】:

      如果有人还在看这个,由于某种原因,Google API 发生了变化,Geocoder 不再适用于标准配置文件。但是,您可以简单地不使用 Geocoder gem 进行地理编码和反向地理编码(不要使用 Geocoder.search)并使用任何 http 请求 gem 直接调用 google api,此时使用 RestClient api 调用将是

      response = RestClient.get 'https://maps.googleapis.com/maps/api/geocode/json?address=' + sanitized_query + '&key=' + your_key
      

      过滤后的查询可以是Cupertino, CA 之类的地址,也可以是用于地理编码或反向地理编码的lat=x, lng=y 字符串。无需获得 Google Premier 帐户。

      【讨论】:

        【解决方案4】:

        Geocoder 可以很好地与他们的 Map API 的免费层配合使用。但是,为了使其正常工作,我必须特别是使用此页面注册一个密钥。

        https://console.developers.google.com/flows/enableapi?apiid=geocoding_backend&keyType=SERVER_SIDE


        并设置配置

        # config/initializers/geocoder.rb
        Geocoder.configure(
          api_key: 'KEY_HERE',
          use_https: true
        )
        

        【讨论】:

          【解决方案5】:

          Geocoder 仅支持 Google Premier 帐户的 Google api 密钥。

          在 github 上的自述文件中可以找到:https://github.com/alexreisner/geocoder#google-google-google_premier

          如果您有 Google Premier api 密钥,您只需将其放入初始化程序中:

          # config/initializers/geocoder.rb
             Geocoder.configure(:lookup => :google_premier, :api_key => "...")
          

          您的地理编码器将使用您的首要密钥。

          【讨论】:

            【解决方案6】:
            Geocoder.configure(
              # geocoding service
              lookup: :google,
            
              # geocoding service request timeout (in seconds)
              timeout: 3,
            
              # default units
              units: :km
            )
            

            这对我有用。您可以使用http://www.rubygeocoder.com/ 的地理编码器文档从rails 控制台调用API,而不是从视图/my_map/show.html.erb 中调用它,用<%= @place.address %> 替换地址或城市等

            【讨论】:

              【解决方案7】:

              默认情况下,Geocoder 使用 Google 的地理编码 API 来获取坐标和街道地址。因此,我认为 Google API 密钥应该适用于初始化程序。

              我希望这对你有用。

              【讨论】:

                猜你喜欢
                • 2020-06-30
                • 2020-11-15
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-12-09
                相关资源
                最近更新 更多