Embedded navigation
/*
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-examples
To learn more about each example in this app, including descriptions and links
to documentation, see our docs: https://docs.mapbox.com/ios/navigation/examples/embedded-navigation
*/
import Foundation
import MapboxCoreNavigation
import MapboxNavigation
import MapboxDirections
import 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 = true
calculateDirections()
}
func calculateDirections() {
Directions.shared.calculate(routeOptions) { [weak self] (_, result) in
switch result {
case .failure(let error):
print(error.localizedDescription)
case .success(let response):
guard let strongSelf = self else {
return
}
strongSelf.routeResponse = response
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 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 = 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)
}
}
このexampleは役に立ちましたか?