-
Notifications
You must be signed in to change notification settings - Fork 318
/
Copy pathNavigationRouteOptions.swift
149 lines (123 loc) · 7.12 KB
/
NavigationRouteOptions.swift
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
import Foundation
import CoreLocation
import MapboxDirections
/**
A `NavigationRouteOptions` object specifies turn-by-turn-optimized criteria for results returned by the Mapbox Directions API.
`NavigationRouteOptions` is a subclass of `RouteOptions` that has been optimized for navigation. Pass an instance of this class into the `Directions.calculate(_:completionHandler:)` method.
This class implements the `NSCopying` protocol by round-tripping the object through `JSONEncoder` and `JSONDecoder`. If you subclass `NavigationRouteOptions`, make sure any properties you add are accounted for in `Decodable(from:)` and `Encodable.encode(to:)`. If your subclass contains any customizations that cannot be represented in JSON, make sure the subclass overrides `NSCopying.copy(with:)` to persist those customizations.
`NavigationRouteOptions` is designed to be used with the `Directions` and `NavigationDirections` classes for specifying routing criteria. To customize the user experience in a `NavigationViewController`, use the `NavigationOptions` class.
*/
open class NavigationRouteOptions: RouteOptions, OptimizedForNavigation {
/**
Initializes a navigation route options object for routes between the given waypoints and an optional profile identifier optimized for navigation.
- seealso: `RouteOptions`
*/
public required init(waypoints: [Waypoint], profileIdentifier: ProfileIdentifier? = .automobileAvoidingTraffic, queryItems: [URLQueryItem]? = nil) {
super.init(waypoints: waypoints.map {
$0.coordinateAccuracy = -1
return $0
},
profileIdentifier: profileIdentifier,
queryItems: queryItems)
includesAlternativeRoutes = true
attributeOptions = [.expectedTravelTime, .maximumSpeedLimit]
if profileIdentifier == .cycling {
// https://github.com/mapbox/mapbox-navigation-ios/issues/3495
attributeOptions.update(with: .congestionLevel)
} else {
attributeOptions.update(with: .numericCongestionLevel)
}
includesExitRoundaboutManeuver = true
if profileIdentifier == .automobileAvoidingTraffic {
refreshingEnabled = true
}
optimizeForNavigation()
}
/**
Initializes an equivalent `RouteOptions` object from a `NavigationMapOptions`
- seealso: `NavigationMatchOptions`
*/
public convenience init(navigationMatchOptions options: NavigationMatchOptions) {
self.init(waypoints: options.waypoints, profileIdentifier: options.profileIdentifier)
}
/**
Initializes a navigation route options object for routes between the given locations and an optional profile identifier optimized for navigation.
- seealso: `RouteOptions`
*/
public convenience init(locations: [CLLocation], profileIdentifier: ProfileIdentifier? = .automobileAvoidingTraffic, queryItems: [URLQueryItem]? = nil) {
self.init(waypoints: locations.map { Waypoint(location: $0) }, profileIdentifier: profileIdentifier, queryItems: queryItems)
}
/**
Initializes a route options object for routes between the given geographic coordinates and an optional profile identifier optimized for navigation.
- seealso: `RouteOptions`
*/
public convenience init(coordinates: [CLLocationCoordinate2D], profileIdentifier: ProfileIdentifier? = .automobileAvoidingTraffic, queryItems: [URLQueryItem]? = nil) {
self.init(waypoints: coordinates.map { Waypoint(coordinate: $0) }, profileIdentifier: profileIdentifier, queryItems: queryItems)
}
required public init(from decoder: Decoder) throws {
try super.init(from: decoder)
}
}
/**
A `NavigationMatchOptions` object specifies turn-by-turn-optimized criteria for results returned by the Mapbox Map Matching API.
`NavigationMatchOptions` is a subclass of `MatchOptions` that has been optimized for navigation. Pass an instance of this class into the `Directions.calculateRoutes(matching:completionHandler:).` method.
Note: it is very important you specify the `waypoints` for the route. Usually the only two values for this `IndexSet` will be 0 and the length of the coordinates. Otherwise, all coordinates passed through will be considered waypoints.
*/
open class NavigationMatchOptions: MatchOptions, OptimizedForNavigation {
/**
Initializes a navigation route options object for routes between the given waypoints and an optional profile identifier optimized for navigation.
- seealso: `RouteOptions`
*/
public required init(waypoints: [Waypoint], profileIdentifier: ProfileIdentifier? = .automobileAvoidingTraffic, queryItems: [URLQueryItem]? = nil) {
super.init(waypoints: waypoints.map {
$0.coordinateAccuracy = -1
return $0
},
profileIdentifier: profileIdentifier,
queryItems: queryItems)
attributeOptions = [.numericCongestionLevel, .expectedTravelTime]
if profileIdentifier == .automobile || profileIdentifier == .automobileAvoidingTraffic {
attributeOptions.insert(.maximumSpeedLimit)
}
optimizeForNavigation()
}
/**
Initializes a navigation match options object for routes between the given locations and an optional profile identifier optimized for navigation.
- seealso: `MatchOptions`
*/
public convenience init(locations: [CLLocation], profileIdentifier: ProfileIdentifier? = .automobileAvoidingTraffic, queryItems: [URLQueryItem]? = nil) {
self.init(waypoints: locations.map { Waypoint(location: $0) }, profileIdentifier: profileIdentifier, queryItems: queryItems)
}
/**
Initializes a navigation match options object for routes between the given geographic coordinates and an optional profile identifier optimized for navigation.
- seealso: `MatchOptions`
*/
public convenience init(coordinates: [CLLocationCoordinate2D], profileIdentifier: ProfileIdentifier? = .automobileAvoidingTraffic, queryItems: [URLQueryItem]? = nil) {
self.init(waypoints: coordinates.map { Waypoint(coordinate: $0) }, profileIdentifier: profileIdentifier, queryItems: queryItems)
}
required public init(from decoder: Decoder) throws {
try super.init(from: decoder)
}
}
protocol OptimizedForNavigation: AnyObject {
var includesSteps: Bool { get set }
var routeShapeResolution: RouteShapeResolution { get set }
var shapeFormat: RouteShapeFormat { get set }
var attributeOptions: AttributeOptions { get set }
var locale: Locale { get set }
var distanceMeasurementSystem: MeasurementSystem { get set }
var includesSpokenInstructions: Bool { get set }
var includesVisualInstructions: Bool { get set }
func optimizeForNavigation()
}
extension OptimizedForNavigation {
func optimizeForNavigation() {
shapeFormat = .polyline6
includesSteps = true
routeShapeResolution = .full
includesSpokenInstructions = true
locale = Locale.nationalizedCurrent
distanceMeasurementSystem = .init(NavigationSettings.shared.distanceUnit)
includesVisualInstructions = true
}
}