17
votes

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.

7
I moved to a front end geocoordinate lookup, while still keeping geocoder on the backend as a fallback. Check out my comment on github.com/alexreisner/geocoder/issues/222#issuecomment-7857999 with details if your interested. - Glenn
I believe this is due to the fact that Google API requests with an API key need to be signed: developers.google.com/maps/documentation/business/webservices I pretty sure Geocoder does not signs these urls, as they also require a client id. - Fadi
Sorry my previous comment is incorrect: You will have to use the Google Premier lookup key for Geocoder: github.com/alexreisner/geocoder#google-google-google_premier However Google Premier is quite expensive from what I've heard. - Fadi

7 Answers

6
votes

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.

3
votes

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
)
2
votes

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'],
)
1
votes

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
)
-1
votes

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.

-1
votes
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 %>

-1
votes

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.