Skip to content

Instantly share code, notes, and snippets.

@johnnewman
Created December 11, 2020 16:17
Show Gist options
  • Save johnnewman/8a2d8c5a3cfec66d1af97bf021219576 to your computer and use it in GitHub Desktop.
Save johnnewman/8a2d8c5a3cfec66d1af97bf021219576 to your computer and use it in GitHub Desktop.
Navigation view controller example
//
// ViewController.swift
// EmptyMBNavProject
//
// Created by John Newman on 12/10/20.
//
import MapboxDirections
import MapboxCoreNavigation
import MapboxNavigation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Define two waypoints to travel between
let origin = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.9131752, longitude: -77.0324047), name: "Mapbox")
let destination = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), name: "White House")
// Set options
let routeOptions = NavigationRouteOptions(waypoints: [origin, destination])
// Request a route using MapboxDirections.swift
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
}
// Pass the first generated route to the the NavigationViewController
let service = MapboxNavigationService(
route: route,
routeIndex: 0,
routeOptions: routeOptions,
directions: nil,
locationSource: nil,
eventsManagerType: nil,
simulating: .always,
routerType: nil)
let navigationOptions = NavigationOptions(
styles: nil,
navigationService: service,
voiceController: nil,
topBanner: nil,
bottomBanner: nil
)
let viewController = NavigationViewController(
for: route,
routeIndex: 0,
routeOptions: routeOptions,
navigationOptions: navigationOptions
)
viewController.modalPresentationStyle = .fullScreen
strongSelf.present(viewController, animated: true, completion: nil)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment