Geocoder gem是否可以使用谷歌API密钥?

我正在为我的项目使用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 Geocoding API错误:请求被拒绝。 当我启动应用程序时。 从阅读开始,如果他们选择继续使用gem,似乎其他人切换到雅虎。 我可以将gem配置为使用google api密钥吗? 主要是,我想留意每日查询的数量,以避免超过限制。

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 => "...") 

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

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

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

创建一个文件:config / initializers / geocoder.rb并设置如下:

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

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 ) 

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

我希望这对你有用。

 Geocoder.configure( # geocoding service lookup: :google, # geocoding service request timeout (in seconds) timeout: 3, # default units units: :km ) 

这对我有用。 您可以使用地理编码器doc在http://www.rubygeocoder.com/上调用来自rails控制台的API,而不是使用<%= @place.address %>来查看/my_map/show.html.erb替换地址或城市等。

如果有人仍在查看此内容,由于某种原因,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这样的地址Cupertino, CA也可以是lat=x, lng=y字符串,用于地理编码或反向地理编码。 没有必要获得Google高级帐户。