directions.dart
16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart';
import 'package:json_annotation/json_annotation.dart';
import 'core.dart';
import 'utils.dart';
part 'directions.g.dart';
const _directionsUrl = '/directions/json';
/// https://developers.google.com/maps/documentation/directions/start
class GoogleMapsDirections extends GoogleWebService {
GoogleMapsDirections({
String? apiKey,
String? baseUrl,
Client? httpClient,
Map<String, String>? apiHeaders,
}) : super(
apiKey: apiKey,
baseUrl: baseUrl,
apiPath: _directionsUrl,
httpClient: httpClient,
apiHeaders: apiHeaders,
);
Future<DirectionsResponse> directions(
Object /*Location|String*/ origin,
Object /*Location|String*/ destination, {
TravelMode? travelMode,
List<Waypoint> waypoints = const [],
bool alternatives = false,
@deprecated RouteType? avoid,
List<RouteType> avoids = const [],
String? language,
Unit? units,
String? region,
Object? /*DateTime|num*/ arrivalTime,
Object? /*DateTime|num|String('now')*/ departureTime,
List<TransitMode> transitMode = const [],
TrafficModel? trafficModel,
TransitRoutingPreferences? transitRoutingPreference,
}) async {
final url = buildUrl(
origin: origin,
destination: destination,
travelMode: travelMode,
waypoints: waypoints,
alternatives: alternatives,
avoids: avoids,
language: language,
units: units,
region: region,
arrivalTime: arrivalTime,
departureTime: departureTime,
transitMode: transitMode,
trafficModel: trafficModel,
transitRoutingPreference: transitRoutingPreference,
);
return _decode(await doGet(url, headers: apiHeaders));
}
Future<DirectionsResponse> directionsWithLocation(
Location origin,
Location destination, {
TravelMode? travelMode,
List<Waypoint> waypoints = const [],
bool alternatives = false,
@deprecated RouteType? avoid,
List<RouteType> avoids = const [],
String? language,
Unit? units,
String? region,
Object? /*DateTime|num*/ arrivalTime,
Object? /*DateTime|num|String('now')*/ departureTime,
List<TransitMode> transitMode = const [],
TrafficModel? trafficModel,
TransitRoutingPreferences? transitRoutingPreference,
}) async {
return directions(
origin,
destination,
travelMode: travelMode,
waypoints: waypoints,
alternatives: alternatives,
avoids: avoids,
language: language,
units: units,
region: region,
arrivalTime: arrivalTime,
departureTime: departureTime,
transitMode: transitMode,
trafficModel: trafficModel,
transitRoutingPreference: transitRoutingPreference,
);
}
Future<DirectionsResponse> directionsWithAddress(
String origin,
String destination, {
TravelMode? travelMode,
List<Waypoint> waypoints = const [],
bool alternatives = false,
@deprecated RouteType? avoid,
List<RouteType> avoids = const [],
String? language,
Unit? units,
String? region,
Object? /*DateTime|num*/ arrivalTime,
Object? /*DateTime|num|String('now')*/ departureTime,
List<TransitMode> transitMode = const [],
TrafficModel? trafficModel,
TransitRoutingPreferences? transitRoutingPreference,
}) async {
return directions(
origin,
destination,
travelMode: travelMode,
waypoints: waypoints,
alternatives: alternatives,
avoids: avoids,
language: language,
units: units,
region: region,
arrivalTime: arrivalTime,
departureTime: departureTime,
transitMode: transitMode,
trafficModel: trafficModel,
transitRoutingPreference: transitRoutingPreference,
);
}
String buildUrl({
required Object /*Location|String*/ origin,
required Object /*Location|String*/ destination,
TravelMode? travelMode,
List<Waypoint> waypoints = const <Waypoint>[],
bool alternatives = false,
@deprecated RouteType? avoid,
List<RouteType> avoids = const <RouteType>[],
String? language,
Unit? units,
String? region,
Object? /*DateTime|num*/ arrivalTime,
Object? /*DateTime|num|String('now')*/ departureTime,
List<TransitMode> transitMode = const <TransitMode>[],
TrafficModel? trafficModel,
TransitRoutingPreferences? transitRoutingPreference,
}) {
final params = <String, String>{};
if (origin is! Location && origin is! String) {
throw ArgumentError("'origin' must be a '$String' or a '$Location'");
}
params['origin'] = origin.toString();
if (destination is! Location && destination is! String) {
throw ArgumentError("'destination' must be a '$String' or a '$Location'");
}
params['destination'] = destination.toString();
if (departureTime != null) {
if (departureTime is! DateTime &&
departureTime is! num &&
departureTime != 'now') {
throw ArgumentError(
"'departureTime' must be a '$num' or a '$DateTime'");
}
params['departure_time'] = departureTime is DateTime
? (departureTime.millisecondsSinceEpoch ~/ 1000).toString()
: departureTime.toString();
}
if (arrivalTime != null) {
if (arrivalTime is! DateTime && arrivalTime is! num) {
throw ArgumentError("'arrivalTime' must be a '$num' or a '$DateTime'");
}
params['arrival_time'] = arrivalTime is DateTime
? (arrivalTime.millisecondsSinceEpoch ~/ 1000).toString()
: arrivalTime.toString();
}
if (waypoints.isNotEmpty == true) {
if (alternatives == true) {
throw ArgumentError(
"'alternatives' is only available for requests without intermediate waypoints",
);
}
params['waypoints'] = waypoints.join('|');
}
if (travelMode != null) {
params['mode'] = travelMode.toApiString();
}
if (alternatives) {
params['alternatives'] = alternatives.toString();
}
if (avoid != null) {
avoids = [
...avoids,
avoid,
];
}
if (avoids.isNotEmpty) {
params['avoid'] = avoids.map((t) => t.toApiString()).join('|');
}
if (language != null) {
params['language'] = language;
}
if (units != null) {
params['units'] = units.toApiString();
}
if (region != null) {
params['region'] = region;
}
if (trafficModel != null) {
params['traffic_model'] = trafficModel.toApiString();
}
if (transitMode.isNotEmpty) {
params['transit_mode'] =
transitMode.map((t) => t.toApiString()).join('|');
}
if (transitRoutingPreference != null) {
params['transit_routing_preference'] =
transitRoutingPreference.toApiString();
}
if (apiKey != null) {
params['key'] = apiKey!;
}
return url.replace(queryParameters: params).toString();
}
DirectionsResponse _decode(Response res) =>
DirectionsResponse.fromJson(json.decode(res.body));
}
@JsonSerializable()
class DirectionsResponse extends GoogleResponseStatus {
/// JSON geocoded_waypos
@JsonKey(defaultValue: <GeocodedWaypoint>[])
final List<GeocodedWaypoint> geocodedWaypoints;
@JsonKey(defaultValue: <Route>[])
final List<Route> routes;
DirectionsResponse({
required String status,
String? errorMessage,
required this.geocodedWaypoints,
required this.routes,
}) : super(status: status, errorMessage: errorMessage);
factory DirectionsResponse.fromJson(Map<String, dynamic> json) =>
_$DirectionsResponseFromJson(json);
Map<String, dynamic> toJson() => _$DirectionsResponseToJson(this);
}
@JsonSerializable()
class Waypoint {
final String value;
Waypoint({required this.value});
static Waypoint fromAddress(String address) => Waypoint(value: address);
static Waypoint fromLocation(Location location) =>
Waypoint(value: location.toString());
static Waypoint fromPlaceId(String placeId) =>
Waypoint(value: 'place_id:$placeId');
static Waypoint fromEncodedPolyline(String polyline) =>
Waypoint(value: 'enc:$polyline:');
static Waypoint optimize() => Waypoint(value: 'optimize:true');
@override
String toString() => value;
factory Waypoint.fromJson(Map<String, dynamic> json) =>
_$WaypointFromJson(json);
Map<String, dynamic> toJson() => _$WaypointToJson(this);
}
@JsonSerializable()
class GeocodedWaypoint {
/// JSON geocoder_status
final String geocoderStatus;
/// JSON place_id
final String placeId;
@JsonKey(defaultValue: <String>[])
final List<String> types;
/// JSON partial_match
@JsonKey(defaultValue: false)
final bool partialMatch;
GeocodedWaypoint({
required this.geocoderStatus,
required this.placeId,
this.types = const <String>[],
this.partialMatch = false,
});
factory GeocodedWaypoint.fromJson(Map<String, dynamic> json) =>
_$GeocodedWaypointFromJson(json);
Map<String, dynamic> toJson() => _$GeocodedWaypointToJson(this);
}
@JsonSerializable()
class Route {
final String summary;
@JsonKey(defaultValue: <Leg>[])
final List<Leg> legs;
final String copyrights;
/// JSON overview_polyline
final Polyline overviewPolyline;
final List warnings;
/// JSON waypoint_order
@JsonKey(defaultValue: <num>[])
final List<num> waypointOrder;
final Bounds bounds;
final Fare? fare;
Route({
required this.summary,
required this.legs,
required this.copyrights,
required this.overviewPolyline,
required this.warnings,
required this.waypointOrder,
required this.bounds,
this.fare,
});
factory Route.fromJson(Map<String, dynamic> json) => _$RouteFromJson(json);
Map<String, dynamic> toJson() => _$RouteToJson(this);
}
abstract class _Step {
/// JSON start_location
final Location startLocation;
/// JSON end_location
final Location endLocation;
final Value duration;
final Value distance;
_Step({
required this.startLocation,
required this.endLocation,
required this.duration,
required this.distance,
});
}
@JsonSerializable()
class Leg extends _Step {
@JsonKey(defaultValue: <Step>[])
final List<Step> steps;
/// JSON start_address
final String startAddress;
/// JSON end_address
final String endAddress;
/// JSON duration_in_traffic
final Value? durationInTraffic;
/// JSON arrival_time
final Time? arrivalTime;
/// JSON departure_time
final Time? departureTime;
Leg({
required this.steps,
required this.startAddress,
required this.endAddress,
this.durationInTraffic,
this.arrivalTime,
this.departureTime,
required Location startLocation,
required Location endLocation,
required Value duration,
required Value distance,
}) : super(
startLocation: startLocation,
endLocation: endLocation,
duration: duration,
distance: distance,
);
factory Leg.fromJson(Map<String, dynamic> json) => _$LegFromJson(json);
Map<String, dynamic> toJson() => _$LegToJson(this);
}
@JsonSerializable()
class Step extends _Step {
/// JSON travel_mode
final TravelMode travelMode;
/// JSON html_instructions
final String htmlInstructions;
final String? maneuver;
final Polyline polyline;
/// JSON transit_details
final TransitDetails? transitDetails;
Step({
required this.travelMode,
required this.htmlInstructions,
required this.polyline,
required Location startLocation,
required Location endLocation,
required Value duration,
required Value distance,
this.transitDetails,
this.maneuver,
}) : super(
startLocation: startLocation,
endLocation: endLocation,
duration: duration,
distance: distance,
);
factory Step.fromJson(Map<String, dynamic> json) => _$StepFromJson(json);
Map<String, dynamic> toJson() => _$StepToJson(this);
}
@JsonSerializable()
class Polyline {
final String points;
Polyline({required this.points});
factory Polyline.fromJson(Map<String, dynamic> json) =>
_$PolylineFromJson(json);
Map<String, dynamic> toJson() => _$PolylineToJson(this);
}
@JsonSerializable()
class Value {
final num value;
final String text;
Value({required this.value, required this.text});
factory Value.fromJson(Map<String, dynamic> json) => _$ValueFromJson(json);
Map<String, dynamic> toJson() => _$ValueToJson(this);
}
@JsonSerializable()
class Fare extends Value {
final String currency;
Fare({required this.currency, required num value, required String text})
: super(value: value, text: text);
factory Fare.fromJson(Map<String, dynamic> json) => _$FareFromJson(json);
@override
Map<String, dynamic> toJson() => _$FareToJson(this);
}
@JsonSerializable()
class Time extends Value {
/// JSON time_zone
final String timeZone;
Time({required this.timeZone, required num value, required String text})
: super(value: value, text: text);
factory Time.fromJson(Map<String, dynamic> json) => _$TimeFromJson(json);
@override
Map<String, dynamic> toJson() => _$TimeToJson(this);
}
@JsonSerializable()
class TransitDetails {
/// JSON arrival_stop
final Stop arrivalStop;
/// JSON departure_stop
final Stop departureStop;
/// JSON arrival_time
final Time arrivalTime;
/// JSON departure_time
final Time departureTime;
final String headsign;
final num headway;
/// JSON num_stops
final num numStops;
TransitDetails({
required this.arrivalStop,
required this.departureStop,
required this.arrivalTime,
required this.departureTime,
required this.headsign,
required this.headway,
required this.numStops,
});
factory TransitDetails.fromJson(Map<String, dynamic> json) =>
_$TransitDetailsFromJson(json);
Map<String, dynamic> toJson() => _$TransitDetailsToJson(this);
}
@JsonSerializable()
class Stop {
final String name;
final Location location;
Stop({required this.name, required this.location});
factory Stop.fromJson(Map<String, dynamic> json) => _$StopFromJson(json);
Map<String, dynamic> toJson() => _$StopToJson(this);
}
@JsonSerializable()
class Line {
final String name;
/// JSON short_name
final String shortName;
final String color;
final List<TransitAgency> agencies;
final String url;
final String icon;
/// JSON text_color
final String textColor;
final VehicleType vehicle;
Line({
required this.name,
required this.shortName,
required this.color,
required this.agencies,
required this.url,
required this.icon,
required this.textColor,
required this.vehicle,
});
factory Line.fromJson(Map<String, dynamic> json) => _$LineFromJson(json);
Map<String, dynamic> toJson() => _$LineToJson(this);
}
@JsonSerializable()
class TransitAgency {
final String name;
final String url;
final String phone;
TransitAgency({
required this.name,
required this.url,
required this.phone,
});
factory TransitAgency.fromJson(Map<String, dynamic> json) =>
_$TransitAgencyFromJson(json);
Map<String, dynamic> toJson() => _$TransitAgencyToJson(this);
}
@JsonSerializable()
class VehicleType {
final String name;
final String type;
final String icon;
/// JSON local_icon
final String localIcon;
VehicleType({
required this.name,
required this.type,
required this.icon,
required this.localIcon,
});
factory VehicleType.fromJson(Map<String, dynamic> json) =>
_$VehicleTypeFromJson(json);
Map<String, dynamic> toJson() => _$VehicleTypeToJson(this);
bool isType(String type) => type.toLowerCase() == this.type.toLowerCase();
static const rail = 'RAIL';
static const metroRail = 'METRO_RAIL';
static const subway = 'SUBWAY';
static const tram = 'TRAM';
static const monorail = 'MONORAIL';
static const heavyRail = 'HEAVY_RAIL';
static const commuterTrain = 'COMMUTER_TRAIN';
static const highSpeedTrain = 'HIGH_SPEED_TRAI';
static const bus = 'BUS';
static const intercityBus = 'INTERCITY_BUS';
static const trolleyBus = 'TROLLEYBUS';
static const shareTaxi = 'SHARE_TAXI';
static const ferry = 'FERRY';
static const cableCar = 'CABLE_CARE';
static const gondolaLift = 'GONDOLA_LIFT';
static const funicular = 'FUNICULAR';
static const other = 'OTHER';
}