Does Geocoder gem work with google API key?
Asked Answered
C

7

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.

Corticosterone answered 31/7, 2012 at 23:12 Comment(3)
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.Newsstand
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.Kerf
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.Kerf
C
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.

Creep answered 20/9, 2013 at 4:1 Comment(0)
O
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
)
Osculation answered 2/12, 2015 at 14:58 Comment(0)
O
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'],
)
Oeo answered 18/6, 2015 at 16:4 Comment(0)
T
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
)
Troostite answered 18/4, 2017 at 19:39 Comment(0)
T
-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.

Toulon answered 1/4, 2013 at 16:15 Comment(0)
V
-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 %>

Vitiligo answered 1/8, 2013 at 21:9 Comment(0)
M
-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.

Materialism answered 29/1, 2016 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.