Does Geocoder gem work with google API key?

北城余情 提交于 2019-12-03 16:36:54

问题


I am using ruby geocoder gem for my project and as the project is growing I am starting to look into connecting to the Google API key. After adding this to the project:

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 

I get Google Geocoding API error: request denied. when I start the application. From reading around, it seems like others switch over to yahoo if they choose to continue using the gem. Can I configure the gem to work with google api key? Mainly, I would like to keep an eye out for the amount of daily queries to avoid going over the limit.


回答1:


Geocoder supports Google api keys for Google Premier accounts only.

Its found here in the readme on github: https://github.com/alexreisner/geocoder#google-google-google_premier

If you have a Google Premier api key you just need to put this in an intializer:

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

And your Geocoder will use your premier key.




回答2:


I had this issue today and managed to solve it by setting use_https e.g.

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



回答3:


create a file: config/initializers/geocoder.rb and setup like this:

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



回答4:


Geocoder works fine with the free tier of their Map API. However, to make it work I had to register a key using this page specifically.

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


And set up the configuration

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



回答5:


By default Geocoder uses Google's geocoding API to fetch coordinates and street addresses. So, I think that a Google API key should work on the initializer.

I hope this work for you.




回答6:


Geocoder.configure(
  # geocoding service
  lookup: :google,

  # geocoding service request timeout (in seconds)
  timeout: 3,

  # default units
  units: :km
)

This work for me. You can call API from rails console with geocoder doc at http://www.rubygeocoder.com/ than call it from view /my_map/show.html.erb replace address or city etc with <%= @place.address %>




回答7:


If anyone is still looking at this, for some reason the Google API changed and Geocoder no longer works with the standard config file. However, you can simply not use the Geocoder gem for geocoding and reverse geocoding (don't use Geocoder.search) and use any http request gem to directly call the google api, as of this moment using RestClient the api call would be

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

where sanitized query can be either an address like Cupertino, CA or a lat=x, lng=y string for geocoding or reverse geocoding. It is not necessary to get a Google premier account.



来源:https://stackoverflow.com/questions/11750370/does-geocoder-gem-work-with-google-api-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!