Google Maps
Installation
gem install google-maps
Or add the following line to your Gemfile:
gem 'google-maps'
Configuration
You can either authenticate with an API key or a digital signature.
API key:
Google::Maps.configure do |config|
config.authentication_mode = Google::Maps::Configuration::API_KEY
config.api_key = 'xxxxxxxxxxx'
end
Digital signature:
Google::Maps.configure do |config|
config.authentication_mode = Google::Maps::Configuration::DIGITAL_SIGNATURE
config.client_id = 'xxxxxxxxxxx'
config.client_secret = 'xxxxxxxxxxx'
end
Default language
Set the default language.
Google::Maps.configure do |config|
config.default_language = :nl
end
Usage Examples
Distance
Google::Maps.distance("Science Park, Amsterdam", "Deventer")
Google::Maps.distance("52.3545543,4.9519029", "Deventer")
Duration
Google::Maps.duration("Science Park, Amsterdam", "Deventer")
Google::Maps.duration("52.3545543,4.9519029", "Deventer")
Distance matrix
Google::Maps.distance_matrix("Science Park, Amsterdam", "Deventer").distance
Google::Maps.distance_matrix("Science Park, Amsterdam", "Deventer").duration
Route
route = Google::Maps.route("Science Park, Amsterdam", "Deventer")
route = Google::Maps.route("52.3545543,4.9519029", "Deventer")
route.distance.text
route.duration.text
route.distance.value
route.duration.value
route.steps
{
"distance" => {
"text" => "0,1 km",
"value" => 125
},
"duration" => {
"text" => "1 min.",
"value" => 35
},
"end_location" => {
"lat" => 52.3556768,
"lng" => 4.9545739
},
"html_instructions" => "Rijd <b>naar het noordwesten</b>, richting het <b>Science Park</b>",
"polyline" => {
"points" => "oqp~Hqpf]?@?@?@KNOVEHA@A?s@wAQ]Q_@We@?A?ADI"
},
"start_location" => {
"lat" => 52.3549602,
"lng" => 4.9538473
},
"travel_mode" => "DRIVING"
},
{
"distance" => {
"text" => "37 m",
........
Options
The distance, duration and route methods allow you to pass an options hash to the Directions API.
All options can also be set as default_params for these services.
Google::Maps.configure do |config|
...
config.default_params = {
directions_service: {
mode: 'transit',
transit_routing_preference: 'fewer_transfers'
}
}
...
end
The following options are permitted:
{
mode: 'driving',
transit_mode: 'train',
transit_routing_preference: 'fewer_transfers',
avoid: 'tolls',
arrival_time: 1545397430,
departure_time: 1545397430,
}
TRAVEL_MODES = [
'driving',
'bicycling',
'transit',
'walking'
]
TRANSIT_MODES = [
'bus',
'subway',
'train',
'tram',
'rail'
]
TRANSIT_ROUTING_PREFERENCES = [
'less_walking', prefer limited amounts of walking.
'fewer_transfers' prefer a limited number of transfers.
]
AVOID = [
'tolls',
'highways',
'ferries',
'indoor',
]
Places
places = Google::Maps.places('Amsterdam')
places.first.text
places.first.place_id
place = Google::Maps.place("ChIJVXealLU_xkcRja_At0z9AGY")
place.place_id
place.latitude
place.longitude
place.address
place.name
place.photos
#<Google::Maps::Result height, html_attribution[], photo_reference, width>
place.website
place.url
Geocode
geocodes = Google::Maps.geocode("Science Park, Amsterdam")
geocodes.first.address = "Science Park, 1012 WX Amsterdam, Nederland"
geocodes.first.latitude = 52.3545543
geocodes.first.longitude = 4.9540916
Testing
Run all tests:
rspec spec/
Copyright
Copyright (c) 2011-2019 Zilverline.
See LICENSE for details.