17

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.

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

7 Answers7

6

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.

Mike Heijmans
  • 439
  • 4
  • 8
3

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
)
Dorian
  • 7,749
  • 4
  • 38
  • 57
DMH
  • 2,529
  • 3
  • 24
  • 35
2

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'],
)
Dorian
  • 7,749
  • 4
  • 38
  • 57
territorial
  • 145
  • 1
  • 6
2

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
)
Sergio Tapia
  • 9,173
  • 12
  • 35
  • 59
-1

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

Dave Hogan
  • 3,201
  • 6
  • 29
  • 54
ari poya
  • 1
  • 2
-1

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.

usmanali
  • 2,028
  • 2
  • 27
  • 38
SWoo
  • 147
  • 1
  • 3
  • 7