I am using the Rails Geocoder gem to geocode the latitude and longitude of a street address submitted by a user. I would also like to automatically pull a users IP location to populate a home screen with other user submitted addresses near their location.
I'd like to use an API key for IPinfo to get the user's location and a different API key for Google to geocode the lat/lng of the street address. Previously, when I call out two api keys I get a warning message stating the first key was overwritten by the second, probably because they had the same name.
I saw this previous post where a user answered saying they can do it by passing the service as a lookup value but how did they set up the api key in the geocoder.rb config file? Did they change the api name for the second service? Did they have to add in another config file?
Any help at all would be appreciated. My current config file is below. I commented out the IPinfo lookup for now so I can still geocode items in production. Thank you.
if Rails.env.production?
Geocoder.configure(
# Geocoding options
timeout: 5, # geocoding service timeout (secs)
lookup: :google, # name of geocoding service (symbol)
# ip_lookup: :ipinfo_io, # name of IP address geocoding service (symbol)
# api_key: ENV['IPINFO_IO_API_KEY'], #API key for ip lookup service
# language: :en, # ISO-639 language code
use_https: true, # use HTTPS for lookup requests? (if supported)
# http_proxy: nil, # HTTP proxy server (user:pass@host:port)
# https_proxy: nil, # HTTPS proxy server (user:pass@host:port)
api_key: ENV['GOOGLE_GEOCODER_API_KEY'], # API key for geocoding service
# cache: Redis.new, # cache object (must respond to #[], #[]=, and #del)
# cache_prefix: 'geocoder:', # prefix (string) to use for all cache keys
# Exceptions that should not be rescued by default
# (if you want to implement custom error handling);
# supports SocketError and Timeout::Error
# always_raise: [],
# Calculation options
# units: :mi, # :km for kilometers or :mi for miles
# distances: :linear # :spherical or :linear
)
end
After some further searching and configuring I finally figured out how to use multiple API keys with Geocoder on Rails. Answering my own question in case someone else has the same issue.
The Rails Geocoder docs do tell you how to use multiple apis but not exactly how to configure for different services (at least for a noob like me). I ended up having to pass the specific symbol for the service to the option I wanted the service to use like:
lookup: :google, # name of geocoding service (symbol)
google: {
api_key: ENV['GOOGLE_GEOCODER_API_KEY'], # API key for geocoding service
}
Please see full code below.
if Rails.env.production?
Geocoder.configure(
# Geocoding options
timeout: 5, # geocoding service timeout (secs)
lookup: :google, # name of geocoding service (symbol)
google: {
api_key: ENV['GOOGLE_GEOCODER_API_KEY'], # API key for geocoding service
},
ip_lookup: :ipinfo_io, # name of IP address geocoding service (symbol)
ipinfo_io: {
api_key: ENV['IPINFO_IO_API_KEY'], #API key for ip lookup service
},
# language: :en, # ISO-639 language code
use_https: true, # use HTTPS for lookup requests? (if supported)
# http_proxy: nil, # HTTP proxy server (user:pass@host:port)
# https_proxy: nil, # HTTPS proxy server (user:pass@host:port)
# cache: Redis.new, # cache object (must respond to #[], #[]=, and #del)
# cache_prefix: 'geocoder:', # prefix (string) to use for all cache keys
# Exceptions that should not be rescued by default
# (if you want to implement custom error handling);
# supports SocketError and Timeout::Error
# always_raise: [],
# Calculation options
# units: :mi, # :km for kilometers or :mi for miles
# distances: :linear # :spherical or :linear
)
end