Search code examples
ruby-on-railsrubygoogle-apigoogle-geocoderrails-geocoder

How does Rails Geocoder Gem access Google API?


I know that we need to use a unique API key to access the Google Geocoding API.I am using the Rails Geocoder Gem in my application and found out that it uses the Google Geocoding API.I was unable to find any configuration files that define the API keys to access the Google API.How does the Geocoder gem access the Google API's.


Solution

  • Geocoder.configure(
      :lookup => :google_premier,
      :api_key => [ 'GOOGLE_CRYPTO_KEY', 'GOOGLE_CLIENT_ID', 'GOOGLE_CHANNEL' ],
      :timeout => 5,
      :units => :km,
    )
    

    https://github.com/alexreisner/geocoder

    Here is one more link : http://hankstoever.com/posts/11-Pro-Tips-for-Using-Geocoder-with-Rails

    under Some common configuration options are:

    You should look into this answer : Does Geocoder gem work with google API key?

    It says:

    Geocoder supports Google api keys for Google Premier accounts only.

    But you can use the Client-Side framework to do that, instead of putting it on the server

    https://developers.google.com/maps/articles/geocodestrat

    I wrote something like that a few days back in ruby, if it helps :

    require 'net/http'
    require "resolv-replace.rb"
    require 'json'
    
    url = URI("https://maps.googleapis.com/maps/api/geocode/json")
    puts "enter country code"
    c_code = gets
    puts "enter zip code "
    zip = gets
    
    url.query= URI.encode_www_form({ address: "#{c_code.chomp}+#{zip.chomp}" })
    res = Net::HTTP::get_response(url)
    
    json_data = res.body if res.is_a?(Net::HTTPSuccess)
    
    data = JSON.parse(json_data)
    p data