google_maps_service_ruby 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +72 -0
- data/CODE_OF_CONDUCT.md +133 -0
- data/LICENSE +202 -0
- data/README.md +375 -0
- data/lib/google_maps_service/apis/directions.rb +99 -0
- data/lib/google_maps_service/apis/distance_matrix.rb +83 -0
- data/lib/google_maps_service/apis/elevation.rb +56 -0
- data/lib/google_maps_service/apis/geocoding.rb +82 -0
- data/lib/google_maps_service/apis/roads.rb +183 -0
- data/lib/google_maps_service/apis/time_zone.rb +39 -0
- data/lib/google_maps_service/apis.rb +5 -0
- data/lib/google_maps_service/client.rb +255 -0
- data/lib/google_maps_service/convert.rb +174 -0
- data/lib/google_maps_service/errors.rb +52 -0
- data/lib/google_maps_service/polyline.rb +89 -0
- data/lib/google_maps_service/url.rb +62 -0
- data/lib/google_maps_service/validator.rb +38 -0
- data/lib/google_maps_service/version.rb +23 -0
- data/lib/google_maps_service.rb +40 -0
- data/lib/google_maps_service_ruby.rb +3 -0
- metadata +210 -0
data/README.md
ADDED
@@ -0,0 +1,375 @@
|
|
1
|
+
# Ruby gem for Google Maps APIs
|
2
|
+
|
3
|
+
 [](https://coveralls.io/github/langsharpe/google-maps-services-ruby?branch=master)
|
4
|
+
|
5
|
+
*This gem is ported from [Python Client for Google Maps Services](https://github.com/googlemaps/google-maps-services-python).*
|
6
|
+
|
7
|
+
## Description
|
8
|
+
|
9
|
+
Use Ruby? Want to [geocode][Geocoding API] something? Looking for [directions][Directions API]?
|
10
|
+
Maybe [matrices of directions][Distance Matrix API]? This gem brings the [Google Maps API Web
|
11
|
+
Services] to your Ruby application.
|
12
|
+
|
13
|
+
The Ruby gem for Google Maps Web Service APIs is a gem for the following Google Maps APIs:
|
14
|
+
|
15
|
+
- [Google Maps Directions API][Directions API]
|
16
|
+
- [Google Maps Distance Matrix API][Distance Matrix API]
|
17
|
+
- [Google Maps Elevation API][Elevation API]
|
18
|
+
- [Google Maps Geocoding API][Geocoding API]
|
19
|
+
- [Google Maps Time Zone API][Time Zone API]
|
20
|
+
- [Google Maps Roads API][Roads API]
|
21
|
+
|
22
|
+
Keep in mind that the same [terms and conditions](https://developers.google.com/maps/terms) apply
|
23
|
+
to usage of the APIs when they're accessed through this gem.
|
24
|
+
|
25
|
+
## Features
|
26
|
+
|
27
|
+
### Rate Limiting
|
28
|
+
|
29
|
+
Never sleep between requests again! By default, requests are sent at the expected rate limits for
|
30
|
+
each web service, typically 10 queries per second for free users. If you want to speed up or slowdown requests, you can do that too, using `queries_per_second` options while initializing API client.
|
31
|
+
|
32
|
+
### Retry on Failure
|
33
|
+
|
34
|
+
Automatically retry when intermittent failures occur. That is, when any of the retriable 5xx errors
|
35
|
+
are returned from the API.
|
36
|
+
|
37
|
+
### Keys *and* Client IDs
|
38
|
+
|
39
|
+
Maps API for Work customers can use their [client ID and secret][clientid] to authenticate. Free
|
40
|
+
customers can use their [API key][apikey], too.
|
41
|
+
|
42
|
+
Note: Currently, [Roads API] does not accept client ID. It requires API key to authenticate the request.
|
43
|
+
|
44
|
+
### Ruby Hash/Array as API Result
|
45
|
+
|
46
|
+
This gem return a Ruby Hash/Array object as the API result. The result format structure is same as in Google Maps API documentation.
|
47
|
+
|
48
|
+
## Requirements
|
49
|
+
|
50
|
+
- Ruby 2.7 or later.
|
51
|
+
- A Google Maps API credentials (API keys or client IDs)
|
52
|
+
|
53
|
+
### Obtain API keys
|
54
|
+
|
55
|
+
Each Google Maps Web Service requires an API key or Client ID. API keys are
|
56
|
+
freely available with a Google Account at https://developers.google.com/console.
|
57
|
+
To generate a server key for your project:
|
58
|
+
|
59
|
+
1. Visit https://developers.google.com/console and log in with
|
60
|
+
a Google Account.
|
61
|
+
1. Select an existing project, or create a new project.
|
62
|
+
1. Select APIs and Services > Enables APIs and services.
|
63
|
+
1. Click Enable APIs and services
|
64
|
+
1. Browse for the API, and set its status to "On". The Ruby Client for Google Maps Services
|
65
|
+
accesses the following APIs:
|
66
|
+
* Directions API
|
67
|
+
* Distance Matrix API
|
68
|
+
* Elevation API
|
69
|
+
* Geocoding API
|
70
|
+
* Time Zone API
|
71
|
+
* Roads API
|
72
|
+
1. Once you've enabled the APIs, click **Credentials** from the left navigation of the Developer
|
73
|
+
Console.
|
74
|
+
1. In the "Public API access", click **Create new Key**.
|
75
|
+
1. Choose **Server Key**.
|
76
|
+
1. If you'd like to restrict requests to a specific IP address, do so now.
|
77
|
+
1. Click **Create**.
|
78
|
+
|
79
|
+
Your API key should be 40 characters long, and begin with `AIza`.
|
80
|
+
|
81
|
+
**Important:** This key should be kept secret on your server.
|
82
|
+
|
83
|
+
## Installation
|
84
|
+
|
85
|
+
Add this line to your application's Gemfile:
|
86
|
+
|
87
|
+
gem 'google_maps_service'
|
88
|
+
|
89
|
+
And then execute:
|
90
|
+
|
91
|
+
bundle install
|
92
|
+
|
93
|
+
Or install it yourself as:
|
94
|
+
|
95
|
+
gem install google_maps_service
|
96
|
+
|
97
|
+
In your Ruby code, add this line to load this gem:
|
98
|
+
|
99
|
+
require 'google_maps_service'
|
100
|
+
|
101
|
+
## Usage
|
102
|
+
|
103
|
+
Before you request Google Maps API, you must configure the client.
|
104
|
+
|
105
|
+
You can view the [reference documentation](http://www.rubydoc.info/gems/google_maps_service).
|
106
|
+
|
107
|
+
### Configure client
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
require 'google_maps_service'
|
111
|
+
|
112
|
+
# Setup API keys
|
113
|
+
gmaps = GoogleMapsService::Client.new(key: 'Add your key here')
|
114
|
+
|
115
|
+
# Setup client IDs
|
116
|
+
gmaps = GoogleMapsService::Client.new(
|
117
|
+
client_id: 'Add your client id here',
|
118
|
+
client_secret: 'Add your client secret here'
|
119
|
+
)
|
120
|
+
|
121
|
+
# More complex setup
|
122
|
+
gmaps = GoogleMapsService::Client.new(
|
123
|
+
key: 'Add your key here',
|
124
|
+
retry_timeout: 20, # Timeout for retrying failed request
|
125
|
+
queries_per_second: 10 # Limit total request per second
|
126
|
+
)
|
127
|
+
```
|
128
|
+
You can also set up the client globally.
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
require 'google_maps_service'
|
132
|
+
|
133
|
+
# Setup global parameters
|
134
|
+
GoogleMapsService.configure do |config|
|
135
|
+
config.key = 'Add your key here'
|
136
|
+
config.retry_timeout = 20
|
137
|
+
config.queries_per_second = 10
|
138
|
+
end
|
139
|
+
|
140
|
+
# Initialize client using global parameters
|
141
|
+
gmaps = GoogleMapsService::Client.new
|
142
|
+
```
|
143
|
+
|
144
|
+
For more examples and detail (setup **proxy**, **timeout**, **caching**, etc.) while initializing the client, check out [Client documentation](http://www.rubydoc.info/gems/google_maps_service/GoogleMapsService/Apis/Client#initialize-instance_method).
|
145
|
+
|
146
|
+
### Latitude/longitude pairs format
|
147
|
+
|
148
|
+
Some APIs require latitude/longitude pair(s) as their parameter(s). This gem accept various format of latitude/longitude pairs:
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
# Array
|
152
|
+
latlng = [40.714224, -73.961452]
|
153
|
+
|
154
|
+
# Hash with symbolized keys
|
155
|
+
latlng = {lat: 40.714224, lng: -73.961452}
|
156
|
+
latlng = {latitude: 40.714224, longitude: -73.961452}
|
157
|
+
|
158
|
+
# Hash with string keys
|
159
|
+
latlng = {'lat' => 40.714224, 'lng' => -73.961452}
|
160
|
+
latlng = {'latitude' => 40.714224, 'longitude' => -73.961452}
|
161
|
+
```
|
162
|
+
|
163
|
+
### Directions API
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
# Simple directions
|
167
|
+
routes = gmaps.directions(
|
168
|
+
'1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA',
|
169
|
+
'2400 Amphitheatre Parkway, Mountain View, CA 94043, USA',
|
170
|
+
mode: 'walking',
|
171
|
+
alternatives: false)
|
172
|
+
```
|
173
|
+
|
174
|
+
Sample result:
|
175
|
+
|
176
|
+
```ruby
|
177
|
+
[{
|
178
|
+
:bounds=>{
|
179
|
+
:northeast=>{:lat=>37.4238004, :lng=>-122.084314},
|
180
|
+
:southwest=>{:lat=>37.42277989999999, :lng=>-122.0882019}
|
181
|
+
},
|
182
|
+
:copyrights=>"Map data ©2015 Google",
|
183
|
+
:legs=>[
|
184
|
+
{
|
185
|
+
:distance=>{:text=>"0.2 mi", :value=>393},
|
186
|
+
:duration=>{:text=>"5 mins", :value=>287},
|
187
|
+
:end_address=>"2400 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
|
188
|
+
:end_location=>{:lat=>37.4238004, :lng=>-122.0882019},
|
189
|
+
:start_address=>"1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
|
190
|
+
:start_location=>{:lat=>37.42277989999999, :lng=>-122.084314},
|
191
|
+
:steps=>[
|
192
|
+
{
|
193
|
+
:distance=>{:text=>"223 ft", :value=>68},
|
194
|
+
:duration=>{:text=>"1 min", :value=>49},
|
195
|
+
:end_location=>{:lat=>37.4228653, :lng=>-122.0850785},
|
196
|
+
:html_instructions=>"Head <b>west</b>",
|
197
|
+
:polyline=>{:points=>"kclcF|qchVEdAGx@ALAJ"},
|
198
|
+
:start_location=>{:lat=>37.42277989999999, :lng=>-122.084314},
|
199
|
+
:travel_mode=>"WALKING"
|
200
|
+
}, {
|
201
|
+
:distance=>{:text=>"108 ft", :value=>33},
|
202
|
+
:duration=>{:text=>"1 min", :value=>23},
|
203
|
+
:end_location=>{:lat=>37.423161, :lng=>-122.0850102},
|
204
|
+
:html_instructions=>"Turn <b>right</b> toward <b>Amphitheatre Pkwy</b>",
|
205
|
+
:maneuver=>"turn-right",
|
206
|
+
:polyline=>{:points=>"}clcFvvchVg@IQC"},
|
207
|
+
:start_location=>{:lat=>37.4228653, :lng=>-122.0850785},
|
208
|
+
:travel_mode=>"WALKING"
|
209
|
+
}, {
|
210
|
+
:distance=>{:text=>"407 ft", :value=>124},
|
211
|
+
:duration=>{:text=>"2 mins", :value=>90},
|
212
|
+
:end_location=>{:lat=>37.423396, :lng=>-122.0863768},
|
213
|
+
:html_instructions=>"Turn <b>left</b> onto <b>Amphitheatre Pkwy</b>",
|
214
|
+
:maneuver=>"turn-left",
|
215
|
+
:polyline=>{:points=>"welcFhvchVEf@Eb@C\\EZGp@Il@CRAJAJ"},
|
216
|
+
:start_location=>{:lat=>37.423161, :lng=>-122.0850102},
|
217
|
+
:travel_mode=>"WALKING"
|
218
|
+
}, {
|
219
|
+
:distance=>{:text=>"0.1 mi", :value=>168},
|
220
|
+
:duration=>{:text=>"2 mins", :value=>125},
|
221
|
+
:end_location=>{:lat=>37.4238004, :lng=>-122.0882019},
|
222
|
+
:html_instructions=>
|
223
|
+
"Slight <b>right</b> to stay on <b>Amphitheatre Pkwy</b><div style=\"font-size:0.9em\">Destination will be on the right</div>",
|
224
|
+
:maneuver=>"turn-slight-right",
|
225
|
+
:polyline=>{:points=>"gglcFz~chVGJADAD?DIh@MhAWhBOxACT"},
|
226
|
+
:start_location=>{:lat=>37.423396, :lng=>-122.0863768},
|
227
|
+
:travel_mode=>"WALKING"
|
228
|
+
}
|
229
|
+
],
|
230
|
+
:via_waypoint=>[]
|
231
|
+
}
|
232
|
+
],
|
233
|
+
:overview_polyline=>{:points=>"kclcF|qchVQxCy@MKjA[xCE^IVMz@y@bH"},
|
234
|
+
:summary=>"Amphitheatre Pkwy",
|
235
|
+
:warnings=>["Walking directions are in beta. Use caution – This route may be missing sidewalks or pedestrian paths."],
|
236
|
+
:waypoint_order=>[]
|
237
|
+
}]
|
238
|
+
```
|
239
|
+
|
240
|
+
For more usage examples and result format, check out [gem documentation](http://www.rubydoc.info/gems/google_maps_service/GoogleMapsService/Apis/Directions), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/directions_spec.rb), and [Google Maps Directions API documentation][Directions API].
|
241
|
+
|
242
|
+
### Distance Matrix API
|
243
|
+
|
244
|
+
```ruby
|
245
|
+
# Multiple parameters distance matrix
|
246
|
+
origins = ["Bobcaygeon ON", [41.43206, -81.38992]]
|
247
|
+
destinations = [[43.012486, -83.6964149], {lat: 42.8863855, lng: -78.8781627}]
|
248
|
+
matrix = gmaps.distance_matrix(origins, destinations,
|
249
|
+
mode: 'driving',
|
250
|
+
language: 'en-AU',
|
251
|
+
avoid: 'tolls',
|
252
|
+
units: 'imperial')
|
253
|
+
```
|
254
|
+
|
255
|
+
For more usage examples and result format, check out [gem documentation](http://www.rubydoc.info/gems/google_maps_service/GoogleMapsService/Apis/DistanceMatrix), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/distance_matrix_spec.rb), and [Google Maps Distance Matrix API documentation][Distance Matrix API].
|
256
|
+
|
257
|
+
### Elevation API
|
258
|
+
|
259
|
+
```ruby
|
260
|
+
# Elevation of some locations
|
261
|
+
locations = [[40.714728, -73.998672], [-34.397, 150.644]]
|
262
|
+
results = gmaps.elevation(locations)
|
263
|
+
|
264
|
+
# Elevation along path
|
265
|
+
locations = [[40.714728, -73.998672], [-34.397, 150.644]]
|
266
|
+
results = gmaps.elevation_along_path(locations, 5)
|
267
|
+
```
|
268
|
+
|
269
|
+
For more usage examples and result format, check out [gem documentation](http://www.rubydoc.info/gems/google_maps_service/GoogleMapsService/Apis/Elevation), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/elevation_spec.rb), and [Google Maps Elevation API documentation][Elevation API].
|
270
|
+
|
271
|
+
### Geocoding API
|
272
|
+
|
273
|
+
```ruby
|
274
|
+
# Geocoding an address
|
275
|
+
results = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')
|
276
|
+
|
277
|
+
# Look up an address with reverse geocoding
|
278
|
+
results = gmaps.reverse_geocode([40.714224, -73.961452])
|
279
|
+
```
|
280
|
+
|
281
|
+
For more usage examples and result format, check out [gem documentation](http://www.rubydoc.info/gems/google_maps_service/GoogleMapsService/Apis/Geocoding), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/geocoding_spec.rb), and [Google Maps Geocoding API documentation][Geocoding API].
|
282
|
+
|
283
|
+
### Roads API
|
284
|
+
|
285
|
+
```ruby
|
286
|
+
# Snap to roads
|
287
|
+
path = [
|
288
|
+
[-33.8671, 151.20714],
|
289
|
+
[-33.86708, 151.20683000000002],
|
290
|
+
[-33.867070000000005, 151.20674000000002],
|
291
|
+
[-33.86703, 151.20625]
|
292
|
+
]
|
293
|
+
results = gmaps.snap_to_roads(path, interpolate: true)
|
294
|
+
|
295
|
+
# Snapped speed limits
|
296
|
+
path = [
|
297
|
+
[-33.8671, 151.20714],
|
298
|
+
[-33.86708, 151.20683000000002],
|
299
|
+
[-33.867070000000005, 151.20674000000002],
|
300
|
+
[-33.86703, 151.20625]
|
301
|
+
]
|
302
|
+
results = gmaps.snapped_speed_limits(path)
|
303
|
+
|
304
|
+
# Speed limits
|
305
|
+
place_ids = [
|
306
|
+
'ChIJ0wawjUCuEmsRgfqC5Wd9ARM',
|
307
|
+
'ChIJ6cs2kkCuEmsRUfqC5Wd9ARM'
|
308
|
+
]
|
309
|
+
results = gmaps.speed_limits(place_ids)
|
310
|
+
```
|
311
|
+
|
312
|
+
For more usage examples and result format, check out [gem documentation](http://www.rubydoc.info/gems/google_maps_service/GoogleMapsService/Apis/Roads), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/roads_spec.rb), and [Google Maps Roads API documentation][Roads API].
|
313
|
+
|
314
|
+
### Time Zone API
|
315
|
+
|
316
|
+
```ruby
|
317
|
+
# Current time zone
|
318
|
+
timezone = gmaps.timezone([39.603481, -119.682251])
|
319
|
+
|
320
|
+
# Time zone at certain time
|
321
|
+
timezone = gmaps.timezone([39.603481, -119.682251], timestamp: Time.at(1608))
|
322
|
+
```
|
323
|
+
|
324
|
+
For more usage examples and result format, check out [gem documentation](http://www.rubydoc.info/gems/google_maps_service/GoogleMapsService/Apis/TimeZone), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/time_zone_spec.rb), and [Google Maps Time Zone API documentation][Time Zone API].
|
325
|
+
|
326
|
+
### Polyline encoder/decoder
|
327
|
+
|
328
|
+
[Google Encoded Polyline] is a lossy compression algorithm that allows you to store a series of coordinates as a single string. This format is used in some APIs:
|
329
|
+
|
330
|
+
- [Directions API] encodes the result path.
|
331
|
+
- [Elevation API] also accepts the encoded polyline as request parameter.
|
332
|
+
|
333
|
+
To handle Google Encoded Polyline, this gem provides encoder/decoder:
|
334
|
+
|
335
|
+
```ruby
|
336
|
+
require 'google_maps_service/polyline' # Or, require 'google_maps_service' is enough
|
337
|
+
|
338
|
+
# Decode polyline
|
339
|
+
encoded_path = '_p~iF~ps|U_ulLnnqC_mqNvxq`@'
|
340
|
+
path = GoogleMapsService::Polyline.decode(encoded_path)
|
341
|
+
#=> [{:lat=>38.5, :lng=>-120.2}, {:lat=>40.7, :lng=>-120.95}, {:lat=>43.252, :lng=>-126.45300000000002}]
|
342
|
+
|
343
|
+
# Encode polyline
|
344
|
+
path = [[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]]
|
345
|
+
encoded_path = GoogleMapsService::Polyline.encode(path)
|
346
|
+
#=> "_p~iF~ps|U_ulLnnqC_mqNvxq`@"
|
347
|
+
```
|
348
|
+
|
349
|
+
## Issues and feature suggestions
|
350
|
+
|
351
|
+
If you find a bug, or have a feature suggestion, please [log an issue][issues]. If you'd like to
|
352
|
+
contribute, please read [How to Contribute](#contributing).
|
353
|
+
|
354
|
+
## Contributing
|
355
|
+
|
356
|
+
1. Fork it (https://github.com/langsharpe/google-maps-services-ruby/fork).
|
357
|
+
2. Create your feature branch (`git checkout -b my-new-feature`).
|
358
|
+
3. Commit your changes (`git commit -am 'Add some feature'`).
|
359
|
+
4. Push to the branch (`git push origin my-new-feature`).
|
360
|
+
5. Create a new Pull Request.
|
361
|
+
|
362
|
+
[apikey]: https://developers.google.com/maps/faq#keysystem
|
363
|
+
[clientid]: https://developers.google.com/maps/documentation/business/webservices/auth
|
364
|
+
|
365
|
+
[Google Maps API Web Services]: https://developers.google.com/maps/web-services/overview/
|
366
|
+
[Directions API]: https://developers.google.com/maps/documentation/directions/
|
367
|
+
[Distance Matrix API]: https://developers.google.com/maps/documentation/distancematrix/
|
368
|
+
[Elevation API]: https://developers.google.com/maps/documentation/elevation/
|
369
|
+
[Geocoding API]: https://developers.google.com/maps/documentation/geocoding/
|
370
|
+
[Time Zone API]: https://developers.google.com/maps/documentation/timezone/
|
371
|
+
[Roads API]: https://developers.google.com/maps/documentation/roads/
|
372
|
+
|
373
|
+
[Google Encoded Polyline]: https://developers.google.com/maps/documentation/utilities/polylinealgorithm
|
374
|
+
|
375
|
+
[issues]: https://github.com/langsharpe/google-maps-services-ruby/issues
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require_relative "../validator"
|
2
|
+
|
3
|
+
module GoogleMapsService::Apis
|
4
|
+
# Performs requests to the Google Maps Directions API.
|
5
|
+
module Directions
|
6
|
+
# Get directions between an origin point and a destination point.
|
7
|
+
#
|
8
|
+
# @example Simple directions
|
9
|
+
# routes = client.directions('Sydney', 'Melbourne')
|
10
|
+
#
|
11
|
+
# @example Complex bicycling directions
|
12
|
+
# routes = client.directions('Sydney', 'Melbourne',
|
13
|
+
# mode: 'bicycling',
|
14
|
+
# avoid: ['highways', 'tolls', 'ferries'],
|
15
|
+
# units: 'metric',
|
16
|
+
# region: 'au')
|
17
|
+
#
|
18
|
+
# @example Public transportation directions
|
19
|
+
# an_hour_from_now = Time.now - (1.0/24)
|
20
|
+
# routes = client.directions('Sydney Town Hall', 'Parramatta, NSW',
|
21
|
+
# mode: 'transit',
|
22
|
+
# arrival_time: an_hour_from_now)
|
23
|
+
#
|
24
|
+
# @example Walking with alternative routes
|
25
|
+
# routes = client.directions('Sydney Town Hall', 'Parramatta, NSW',
|
26
|
+
# mode: 'walking',
|
27
|
+
# alternatives: true)
|
28
|
+
#
|
29
|
+
# @param [String, Hash, Array] origin The address or latitude/longitude value from which you wish
|
30
|
+
# to calculate directions.
|
31
|
+
# @param [String, Hash, Array] destination The address or latitude/longitude value from which
|
32
|
+
# you wish to calculate directions.
|
33
|
+
# @param [String] mode Specifies the mode of transport to use when calculating
|
34
|
+
# directions. One of `driving`, `walking`, `bicycling` or `transit`.
|
35
|
+
# @param [Array<String>, Array<Hash>, Array<Array>] waypoints Specifies an array of waypoints. Waypoints alter a
|
36
|
+
# route by routing it through the specified location(s).
|
37
|
+
# @param [Boolean] alternatives If True, more than one route may be returned in the
|
38
|
+
# response.
|
39
|
+
# @param [Array, String] avoid Indicates that the calculated route(s) should avoid the
|
40
|
+
# indicated features.
|
41
|
+
# @param [String] language The language in which to return results.
|
42
|
+
# @param [String] units Specifies the unit system to use when displaying results.
|
43
|
+
# `metric` or `imperial`.
|
44
|
+
# @param [String] region The region code, specified as a ccTLD (_top-level domain_)
|
45
|
+
# two-character value.
|
46
|
+
# @param [Integer, DateTime] departure_time Specifies the desired time of departure.
|
47
|
+
# @param [Integer, DateTime] arrival_time Specifies the desired time of arrival for transit
|
48
|
+
# directions. Note: you can not specify both `departure_time` and
|
49
|
+
# `arrival_time`.
|
50
|
+
# @param [Boolean] optimize_waypoints Optimize the provided route by rearranging the
|
51
|
+
# waypoints in a more efficient order.
|
52
|
+
# @param [String, Array<String>] transit_mode Specifies one or more preferred modes of transit.
|
53
|
+
# This parameter may only be specified for requests where the mode is
|
54
|
+
# transit. Valid values are `bus`, `subway`, `train`, `tram` or `rail`.
|
55
|
+
# `rail` is equivalent to `["train", "tram", "subway"]`.
|
56
|
+
# @param [String] transit_routing_preference Specifies preferences for transit
|
57
|
+
# requests. Valid values are `less_walking` or `fewer_transfers`.
|
58
|
+
#
|
59
|
+
# @return [Array] Array of routes.
|
60
|
+
def directions(origin, destination,
|
61
|
+
mode: nil, waypoints: nil, alternatives: false, avoid: nil,
|
62
|
+
language: nil, units: nil, region: nil, departure_time: nil,
|
63
|
+
arrival_time: nil, optimize_waypoints: false, transit_mode: nil,
|
64
|
+
transit_routing_preference: nil)
|
65
|
+
|
66
|
+
params = {
|
67
|
+
origin: GoogleMapsService::Convert.waypoint(origin),
|
68
|
+
destination: GoogleMapsService::Convert.waypoint(destination)
|
69
|
+
}
|
70
|
+
|
71
|
+
params[:mode] = GoogleMapsService::Validator.travel_mode(mode) if mode
|
72
|
+
|
73
|
+
if waypoints
|
74
|
+
waypoints = GoogleMapsService::Convert.as_list(waypoints)
|
75
|
+
waypoints = waypoints.map { |waypoint| GoogleMapsService::Convert.waypoint(waypoint) }
|
76
|
+
waypoints = ["optimize:true"] + waypoints if optimize_waypoints
|
77
|
+
|
78
|
+
params[:waypoints] = GoogleMapsService::Convert.join_list("|", waypoints)
|
79
|
+
end
|
80
|
+
|
81
|
+
params[:alternatives] = "true" if alternatives
|
82
|
+
params[:avoid] = GoogleMapsService::Convert.join_list("|", avoid) if avoid
|
83
|
+
params[:language] = language if language
|
84
|
+
params[:units] = units if units
|
85
|
+
params[:region] = region if region
|
86
|
+
params[:departure_time] = GoogleMapsService::Convert.time(departure_time) if departure_time
|
87
|
+
params[:arrival_time] = GoogleMapsService::Convert.time(arrival_time) if arrival_time
|
88
|
+
|
89
|
+
if departure_time && arrival_time
|
90
|
+
raise ArgumentError, "Should not specify both departure_time and arrival_time."
|
91
|
+
end
|
92
|
+
|
93
|
+
params[:transit_mode] = GoogleMapsService::Convert.join_list("|", transit_mode) if transit_mode
|
94
|
+
params[:transit_routing_preference] = transit_routing_preference if transit_routing_preference
|
95
|
+
|
96
|
+
get("/maps/api/directions/json", params)[:routes]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require_relative "../validator"
|
2
|
+
|
3
|
+
module GoogleMapsService::Apis
|
4
|
+
# Performs requests to the Google Maps Distance Matrix API.
|
5
|
+
module DistanceMatrix
|
6
|
+
# Gets travel distance and time for a matrix of origins and destinations.
|
7
|
+
#
|
8
|
+
# @example Simple distance matrix
|
9
|
+
# origins = ["Perth, Australia", "Sydney, Australia",
|
10
|
+
# "Melbourne, Australia", "Adelaide, Australia",
|
11
|
+
# "Brisbane, Australia", "Darwin, Australia",
|
12
|
+
# "Hobart, Australia", "Canberra, Australia"]
|
13
|
+
# destinations = ["Uluru, Australia",
|
14
|
+
# "Kakadu, Australia",
|
15
|
+
# "Blue Mountains, Australia",
|
16
|
+
# "Bungle Bungles, Australia",
|
17
|
+
# "The Pinnacles, Australia"]
|
18
|
+
# matrix = client.distance_matrix(origins, destinations)
|
19
|
+
#
|
20
|
+
# @example Complex distance matrix
|
21
|
+
# origins = ["Bobcaygeon ON", [41.43206, -81.38992]]
|
22
|
+
# destinations = [[43.012486, -83.6964149], {lat: 42.8863855, lng: -78.8781627}]
|
23
|
+
# matrix = client.distance_matrix(origins, destinations,
|
24
|
+
# mode: 'driving',
|
25
|
+
# language: 'en-AU',
|
26
|
+
# avoid: 'tolls',
|
27
|
+
# units: 'imperial')
|
28
|
+
#
|
29
|
+
# @param [Array] origins One or more addresses and/or lat/lon pairs,
|
30
|
+
# from which to calculate distance and time. If you pass an address
|
31
|
+
# as a string, the service will geocode the string and convert it to
|
32
|
+
# a lat/lon coordinate to calculate directions.
|
33
|
+
# @param [Array] destinations One or more addresses and/or lat/lon pairs, to
|
34
|
+
# which to calculate distance and time. If you pass an address as a
|
35
|
+
# string, the service will geocode the string and convert it to a
|
36
|
+
# lat/lon coordinate to calculate directions.
|
37
|
+
# @param [String] mode Specifies the mode of transport to use when calculating
|
38
|
+
# directions. Valid values are `driving`, `walking`, `transit` or `bicycling`.
|
39
|
+
# @param [String] language The language in which to return results.
|
40
|
+
# @param [String] avoid Indicates that the calculated route(s) should avoid the
|
41
|
+
# indicated features. Valid values are `tolls`, `highways` or `ferries`.
|
42
|
+
# @param [String] units Specifies the unit system to use when displaying results.
|
43
|
+
# Valid values are `metric` or `imperial`.
|
44
|
+
# @param [Integer, DateTime] departure_time Specifies the desired time of departure.
|
45
|
+
# @param [Integer, DateTime] arrival_time Specifies the desired time of arrival for transit
|
46
|
+
# directions. Note: you can not specify both `departure_time` and `arrival_time`.
|
47
|
+
# @param [String, Array<String>] transit_mode Specifies one or more preferred modes of transit.
|
48
|
+
# This parameter may only be specified for requests where the mode is
|
49
|
+
# transit. Valid values are `bus`, `subway`, `train`, `tram`, or `rail`.
|
50
|
+
# `rail` is equivalent to `["train", "tram", "subway"]`.
|
51
|
+
# @param [String] transit_routing_preference Specifies preferences for transit
|
52
|
+
# requests. Valid values are `less_walking` or `fewer_transfers`.
|
53
|
+
#
|
54
|
+
# @return [Hash] Matrix of distances. Results are returned in rows, each row
|
55
|
+
# containing one origin paired with each destination.
|
56
|
+
def distance_matrix(origins, destinations,
|
57
|
+
mode: nil, language: nil, avoid: nil, units: nil,
|
58
|
+
departure_time: nil, arrival_time: nil, transit_mode: nil,
|
59
|
+
transit_routing_preference: nil)
|
60
|
+
params = {
|
61
|
+
origins: GoogleMapsService::Convert.waypoints(origins),
|
62
|
+
destinations: GoogleMapsService::Convert.waypoints(destinations)
|
63
|
+
}
|
64
|
+
|
65
|
+
params[:language] = language if language
|
66
|
+
params[:mode] = GoogleMapsService::Validator.travel_mode(mode) if mode
|
67
|
+
params[:avoid] = GoogleMapsService::Validator.avoid(avoid) if avoid
|
68
|
+
|
69
|
+
params[:units] = units if units
|
70
|
+
params[:departure_time] = GoogleMapsService::Convert.time(departure_time) if departure_time
|
71
|
+
params[:arrival_time] = GoogleMapsService::Convert.time(arrival_time) if arrival_time
|
72
|
+
|
73
|
+
if departure_time && arrival_time
|
74
|
+
raise ArgumentError, "Should not specify both departure_time and arrival_time."
|
75
|
+
end
|
76
|
+
|
77
|
+
params[:transit_mode] = GoogleMapsService::Convert.join_list("|", transit_mode) if transit_mode
|
78
|
+
params[:transit_routing_preference] = transit_routing_preference if transit_routing_preference
|
79
|
+
|
80
|
+
get("/maps/api/distancematrix/json", params)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module GoogleMapsService::Apis
|
2
|
+
# Performs requests to the Google Maps Elevation API.
|
3
|
+
module Elevation
|
4
|
+
# Provides elevation data for locations provided on the surface of the
|
5
|
+
# earth, including depth locations on the ocean floor (which return negative
|
6
|
+
# values).
|
7
|
+
#
|
8
|
+
# @example Single point elevation
|
9
|
+
# results = client.elevation({latitude: 40.714728, longitude: -73.998672})
|
10
|
+
#
|
11
|
+
# @example Multiple points elevation
|
12
|
+
# locations = [[40.714728, -73.998672], [-34.397, 150.644]]
|
13
|
+
# results = client.elevation(locations)
|
14
|
+
#
|
15
|
+
# @param [Array] locations A single latitude/longitude hash, or an array of
|
16
|
+
# latitude/longitude hash from which you wish to calculate
|
17
|
+
# elevation data.
|
18
|
+
#
|
19
|
+
# @return [Array] Array of elevation data responses
|
20
|
+
def elevation(locations)
|
21
|
+
params = {
|
22
|
+
locations: GoogleMapsService::Convert.waypoints(locations)
|
23
|
+
}
|
24
|
+
|
25
|
+
get("/maps/api/elevation/json", params)[:results]
|
26
|
+
end
|
27
|
+
|
28
|
+
# Provides elevation data sampled along a path on the surface of the earth.
|
29
|
+
#
|
30
|
+
# @example Elevation along path
|
31
|
+
# locations = [[40.714728, -73.998672], [-34.397, 150.644]]
|
32
|
+
# results = client.elevation_along_path(locations, 5)
|
33
|
+
#
|
34
|
+
# @param [String, Array] path A encoded polyline string, or a list of
|
35
|
+
# latitude/longitude pairs from which you wish to calculate
|
36
|
+
# elevation data.
|
37
|
+
# @param [Integer] samples The number of sample points along a path for which to
|
38
|
+
# return elevation data.
|
39
|
+
#
|
40
|
+
# @return [Array] Array of elevation data responses
|
41
|
+
def elevation_along_path(path, samples)
|
42
|
+
path = if path.is_a?(String)
|
43
|
+
"enc:%s" % path
|
44
|
+
else
|
45
|
+
GoogleMapsService::Convert.waypoints(path)
|
46
|
+
end
|
47
|
+
|
48
|
+
params = {
|
49
|
+
path: path,
|
50
|
+
samples: samples
|
51
|
+
}
|
52
|
+
|
53
|
+
get("/maps/api/elevation/json", params)[:results]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|