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