Embedded navigation
Embed navigation into your application.
/*This code example is part of the Mapbox Navigation SDK for iOS demo app,which you can build and run: https://github.com/mapbox/mapbox-navigation-ios-examplesTo learn more about each example in this app, including descriptions and linksto documentation, see our docs: https://docs.mapbox.com/ios/navigation/examples/embedded-navigation*/ import Foundationimport MapboxCoreNavigationimport MapboxNavigationimport MapboxDirectionsimport MapboxMaps class EmbeddedExampleViewController: UIViewController { @IBOutlet weak var reroutedLabel: UILabel!@IBOutlet weak var enableReroutes: UISwitch!@IBOutlet weak var container: UIView!var routeResponse: RouteResponse? lazy var routeOptions: NavigationRouteOptions = {let origin = CLLocationCoordinate2DMake(37.77440680146262, -122.43539772352648)let destination = CLLocationCoordinate2DMake(37.76556957793795, -122.42409811526268)return NavigationRouteOptions(coordinates: [origin, destination])}() override func viewDidLoad() {super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(EmbeddedExampleViewController.flashReroutedLabel(_:)), name: .routeControllerDidReroute, object: nil)reroutedLabel.isHidden = truecalculateDirections()} func calculateDirections() {Directions.shared.calculate(routeOptions) { [weak self] (_, result) inswitch result {case .failure(let error):print(error.localizedDescription)case .success(let response):guard let strongSelf = self else {return} strongSelf.routeResponse = responsestrongSelf.startEmbeddedNavigation()}}} @objc func flashReroutedLabel(_ sender: Any) {reroutedLabel.isHidden = falsereroutedLabel.alpha = 1.0UIView.animate(withDuration: 1.0, delay: 1, options: .curveEaseIn, animations: {self.reroutedLabel.alpha = 0.0}, completion: { _ inself.reroutedLabel.isHidden = true})} func startEmbeddedNavigation() {// For demonstration purposes, simulate locations if the Simulate Navigation option is on.guard let routeResponse = routeResponse else { return }let indexedRouteResponse = IndexedRouteResponse(routeResponse: routeResponse, routeIndex: 0)let navigationService = MapboxNavigationService(indexedRouteResponse: indexedRouteResponse,customRoutingProvider: NavigationSettings.shared.directions,credentials: NavigationSettings.shared.directions.credentials,simulating: simulationIsEnabled ? .always : .onPoorGPS)let navigationOptions = NavigationOptions(navigationService: navigationService)let navigationViewController = NavigationViewController(for: indexedRouteResponse,navigationOptions: navigationOptions) navigationViewController.delegate = selfaddChild(navigationViewController)container.addSubview(navigationViewController.view)navigationViewController.view.translatesAutoresizingMaskIntoConstraints = falseNSLayoutConstraint.activate([navigationViewController.view.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 0),navigationViewController.view.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: 0),navigationViewController.view.topAnchor.constraint(equalTo: container.topAnchor, constant: 0),navigationViewController.view.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: 0)])self.didMove(toParent: self)}} extension EmbeddedExampleViewController: NavigationViewControllerDelegate {func navigationViewController(_ navigationViewController: NavigationViewController, shouldRerouteFrom location: CLLocation) -> Bool {return enableReroutes.isOn} func navigationViewControllerDidDismiss(_ navigationViewController: NavigationViewController, byCanceling canceled: Bool) {navigationController?.popViewController(animated: true)}}