google_map_services 0.1.0.0

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