Rails Geocoder Gem如何访问Google API?

Soo*_*ndu 5 ruby ruby-on-rails google-api google-geocoder rails-geocoder

我知道我们需要使用唯一的API密钥来访问Google地理编码API.我在我的应用程序中使用Rails Geocoder Gem并发现它使用了Google地理编码API.我无法找到任何定义用于访问Google API的API密钥.Geocoder gem如何访问Google API.

arg*_*m47 4

Geocoder.configure(
  :lookup => :google_premier,
  :api_key => [ 'GOOGLE_CRYPTO_KEY', 'GOOGLE_CLIENT_ID', 'GOOGLE_CHANNEL' ],
  :timeout => 5,
  :units => :km,
)
Run Code Online (Sandbox Code Playgroud)

https://github.com/alexreisner/geocoder

这里还有一个链接:http://hankstoever.com/posts/11-Pro-Tips-for-Using-Geocoder-with-Rails

一些常见的配置选项如下

你应该看看这个答案:Does Geocoder gem work with google API key?

它说:

地理编码器仅支持 Google Premier 帐户的 Google api 密钥。

但是您可以使用客户端框架来做到这一点,而不是将其放在服务器上

https://developers.google.com/maps/articles/geocodestrat

几天前我用 ruby​​ 写了类似的东西,如果有帮助的话:

require 'net/http'
require "resolv-replace.rb"
require 'json'

url = URI("https://maps.googleapis.com/maps/api/geocode/json")
puts "enter country code"
c_code = gets
puts "enter zip code "
zip = gets

url.query= URI.encode_www_form({ address: "#{c_code.chomp}+#{zip.chomp}" })
res = Net::HTTP::get_response(url)

json_data = res.body if res.is_a?(Net::HTTPSuccess)

data = JSON.parse(json_data)
p data
Run Code Online (Sandbox Code Playgroud)