|
| 1 | +/* |
| 2 | + This code example is part of the Mapbox Navigation SDK for iOS demo app, |
| 3 | + which you can build and run: https://github.com/mapbox/mapbox-navigation-ios-examples |
| 4 | + To learn more about each example in this app, including descriptions and links |
| 5 | + to documentation, see our docs: https://docs.mapbox.com/ios/navigation/examples/basic |
| 6 | + */ |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import UIKit |
| 10 | +import MapboxCoreNavigation |
| 11 | +import MapboxNavigation |
| 12 | +import MapboxDirections |
| 13 | + |
| 14 | +/* |
| 15 | + This example demonstrates how users can control and customize the rerouting process. |
| 16 | + Unlike `Custom-Server` example, this one does not completely cancel SDK mechanism to do it |
| 17 | + separately, but instead controls just the part of new route calculation, preserving original |
| 18 | + workflow and events. In result, any related components will not know that rerouting was altered |
| 19 | + and will react as on usual reroute event. |
| 20 | + */ |
| 21 | +class CustomRoutingProviderViewController: UIViewController { |
| 22 | + override func viewDidLoad() { |
| 23 | + super.viewDidLoad() |
| 24 | + |
| 25 | + let origin = CLLocationCoordinate2DMake(37.77440680146262, -122.43539772352648) |
| 26 | + let destination = CLLocationCoordinate2DMake(37.76556957793795, -122.42409811526268) |
| 27 | + let options = NavigationRouteOptions(coordinates: [origin, destination]) |
| 28 | + |
| 29 | + // This example is similar to `BasicViewController` except that we are using our custom `RoutingProvider` implementation for retrieving routes. |
| 30 | + let customRoutingProvider = CustomProvider() |
| 31 | + |
| 32 | + _ = customRoutingProvider.calculateRoutes(options: options, completionHandler: { [weak self] (_, result) in |
| 33 | + switch result { |
| 34 | + case .failure(let error): |
| 35 | + print(error.localizedDescription) |
| 36 | + case .success(let response): |
| 37 | + guard let strongSelf = self else { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + let navigationService = MapboxNavigationService(routeResponse: response, |
| 42 | + routeIndex: 0, |
| 43 | + routeOptions: options, |
| 44 | + routingProvider: customRoutingProvider, // passing `customRoutingProvider` to ensure it is used for re-routing and refreshing |
| 45 | + credentials: NavigationSettings.shared.directions.credentials, |
| 46 | + simulating: simulationIsEnabled ? .always : .onPoorGPS) |
| 47 | + |
| 48 | + let navigationOptions = NavigationOptions(navigationService: navigationService) |
| 49 | + let navigationViewController = NavigationViewController(for: response, |
| 50 | + routeIndex: 0, |
| 51 | + routeOptions: options, |
| 52 | + navigationOptions: navigationOptions) |
| 53 | + navigationViewController.modalPresentationStyle = .fullScreen |
| 54 | + navigationViewController.routeLineTracksTraversal = true |
| 55 | + |
| 56 | + strongSelf.present(navigationViewController, animated: true, completion: nil) |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +class CustomProvider: RoutingProvider { |
| 63 | + // This can encapsulate any route building engine we need. For simplicity let's use `MapboxRoutingProvider`. |
| 64 | + let routeCalculator = MapboxRoutingProvider() |
| 65 | + |
| 66 | + // We can also modify the options used to calculate a route. |
| 67 | + func applyOptionsModification(_ options: DirectionsOptions) { |
| 68 | + options.attributeOptions = [.congestionLevel, .speed, .maximumSpeedLimit, .expectedTravelTime] |
| 69 | + } |
| 70 | + |
| 71 | + // Here any manipulations on the reponse data can take place |
| 72 | + func applyMapMatchingModifications(_ response: MapMatchingResponse) { |
| 73 | + response.matches?.forEach { match in |
| 74 | + match.legs.forEach { leg in |
| 75 | + leg.incidents = fetchExternalIncidents(for: leg) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Let's say we have an external source of incidents data, we want to apply to the route. |
| 81 | + func fetchExternalIncidents(for leg: RouteLeg) -> [Incident] { |
| 82 | + return [Incident(identifier: "\(leg.name) incident", |
| 83 | + type: .otherNews, |
| 84 | + description: "Custom Incident", |
| 85 | + creationDate: Date(), |
| 86 | + startDate: Date(), |
| 87 | + endDate: Date().addingTimeInterval(60), |
| 88 | + impact: nil, |
| 89 | + subtype: nil, |
| 90 | + subtypeDescription: nil, |
| 91 | + alertCodes: [], |
| 92 | + lanesBlocked: nil, |
| 93 | + shapeIndexRange: 0..<1)] |
| 94 | + } |
| 95 | + |
| 96 | + // MARK: RoutingProvider implementation |
| 97 | + |
| 98 | + func calculateRoutes(options: RouteOptions, completionHandler: @escaping Directions.RouteCompletionHandler) -> NavigationProviderRequest? { |
| 99 | + applyOptionsModification(options) |
| 100 | + |
| 101 | + // Using `MapboxRoutingProvider` also illustrates cases when we need to modify just a part of the route, or dynamically edit `RouteOptions` for each reroute. |
| 102 | + return routeCalculator.calculateRoutes(options: options, |
| 103 | + completionHandler: completionHandler) |
| 104 | + } |
| 105 | + |
| 106 | + func calculateRoutes(options: MatchOptions, completionHandler: @escaping Directions.MatchCompletionHandler) -> NavigationProviderRequest? { |
| 107 | + applyOptionsModification(options) |
| 108 | + |
| 109 | + return routeCalculator.calculateRoutes(options: options, |
| 110 | + completionHandler: { [weak self] (session, result) in |
| 111 | + switch result { |
| 112 | + case .failure(_): |
| 113 | + completionHandler(session, result) |
| 114 | + case .success(let response): |
| 115 | + guard let strongSelf = self else { |
| 116 | + return |
| 117 | + } |
| 118 | + strongSelf.applyMapMatchingModifications(response) |
| 119 | + |
| 120 | + completionHandler(session, .success(response)) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | + |
| 125 | + // Let's make our custom routing provider prevent route refreshes. |
| 126 | + func refreshRoute(indexedRouteResponse: IndexedRouteResponse, fromLegAtIndex: UInt32, completionHandler: @escaping Directions.RouteCompletionHandler) -> NavigationProviderRequest? { |
| 127 | + |
| 128 | + var options: DirectionsOptions! |
| 129 | + switch indexedRouteResponse.routeResponse.options { |
| 130 | + case.match(let matchOptions): |
| 131 | + options = matchOptions |
| 132 | + case .route(let routeOptions): |
| 133 | + options = routeOptions |
| 134 | + } |
| 135 | + |
| 136 | + completionHandler((options, NavigationSettings.shared.directions.credentials), |
| 137 | + .failure(.unableToRoute)) |
| 138 | + return nil |
| 139 | + } |
| 140 | +} |
0 commit comments