4

我知道我们需要使用唯一的API密钥才能访问Google地理编码API。我在我的应用程序中使用了Rails Geocoder Gem,并发现它使用了Google地理编码API。我无法找到任何配置文件定义访问Google API的API密钥。Geocoder gem如何访问Google API。Rails Geocoder Gem如何访问Google API?

回答

3
Geocoder.configure(
    :lookup => :google_premier, 
    :api_key => [ 'GOOGLE_CRYPTO_KEY', 'GOOGLE_CLIENT_ID', 'GOOGLE_CHANNEL' ], 
    :timeout => 5, 
    :units => :km, 
) 

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?

它说:

Geocoder仅支持谷歌首要帐户的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