Embedded navigation
A newer version of the Navigation SDK is available
This page uses v1.4.2 of the Mapbox Navigation SDK. A newer version of the SDK is available. Learn about the latest version, v2.19.0, in the Navigation SDK documentation.
ViewController
import Foundation
import MapboxCoreNavigation
import MapboxNavigation
import MapboxDirections
class EmbeddedExampleViewController: UIViewController {
@IBOutlet weak var reroutedLabel: UILabel!
@IBOutlet weak var enableReroutes: UISwitch!
@IBOutlet weak var container: UIView!
var route: Route?
lazy var routeOptions: NavigationRouteOptions = {
let origin = CLLocationCoordinate2DMake(37.77440680146262, -122.43539772352648)
let destination = CLLocationCoordinate2DMake(37.76556957793795, -122.42409811526268)
return NavigationRouteOptions(coordinates: [origin, destination])
}()
deinit {
NotificationCenter.default.removeObserver(self)
}
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(EmbeddedExampleViewController.flashReroutedLabel(_:)), name: .routeControllerDidReroute, object: nil)
reroutedLabel.isHidden = true
calculateDirections()
}
func calculateDirections() {
Directions.shared.calculate(routeOptions) { [weak self] (session, result) in
switch result {
case .failure(let error):
print(error.localizedDescription)
case .success(let response):
guard let route = response.routes?.first, let strongSelf = self else {
return
}
strongSelf.route = route
strongSelf.startEmbeddedNavigation()
}
}
}
@objc func flashReroutedLabel(_ sender: Any) {
reroutedLabel.isHidden = false
reroutedLabel.alpha = 1.0
UIView.animate(withDuration: 1.0, delay: 1, options: .curveEaseIn, animations: {
self.reroutedLabel.alpha = 0.0
}, completion: { _ in
self.reroutedLabel.isHidden = true
})
}
func startEmbeddedNavigation() {
// For demonstration purposes, simulate locations if the Simulate Navigation option is on.
guard let route = route else { return }
let navigationService = MapboxNavigationService(route: route, routeIndex: 0, routeOptions: routeOptions, simulating: simulationIsEnabled ? .always : .onPoorGPS)
let navigationOptions = NavigationOptions(navigationService: navigationService)
let navigationViewController = NavigationViewController(for: route, routeIndex: 0, routeOptions: routeOptions, navigationOptions: navigationOptions)
navigationViewController.delegate = self
addChild(navigationViewController)
container.addSubview(navigationViewController.view)
navigationViewController.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.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)
}
}