我一直在努力让 SwiftUI 中的 Mapbox Navigation SDK 工作。我有 UIKit 中版本的工作示例,如文档中所示:https ://docs.mapbox.com/ios/navigation/guides/get-started/install/ 。然而,事实证明这在 SwiftUI 中很难实现。
这是我的代码的样子:
'''
导入 Foundation 导入 SwiftUI 导入 CoreLocation 导入 MapboxDirections 导入 MapboxCoreNavigation 导入 MapboxNavigation
结构导航视图:UIViewControllerRepresentable {
let origin = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.9131752, longitude: -77.0324047), coordinateAccuracy: 20, name: "Mapbox")
let destination = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), coordinateAccuracy: 20, name: "White House")
@Binding var presentedAsModal: Bool
var navigationViewController: NavigationViewController?
func makeCoordinator() -> NavigationView.Coordinator {
Coordinator(self)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<NavigationView>) -> NavigationViewController {
//let directionsRoute = NavigationRouteOptions(waypoints: [origin, destination], profileIdentifier: .automobile)
calculateRoute()
/*
Directions.shared.calculate(directionsRoute) { session, result in
switch result {
case .failure(let error):
print(error.localizedDescription)
case.success(let response):
// Pass the first generated route to the navigationViewController
viewController.self = NavigationViewController(for: response, routeIndex: 0, routeOptions: directionsRoute)
viewController.self!.modalPresentationStyle = .fullScreen
Button("dismiss") {
self.presentedAsModal = false
}
}
}
*/
if navigationViewController != nil {
navigationViewController?.delegate = context.coordinator
}
return navigationViewController!
}
func updateUIViewController(_ uiViewController: NavigationViewController, context: UIViewControllerRepresentableContext<NavigationView>) {
}
func calculateRoute() {
// View Controller:
var viewController: NavigationViewController?
// 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) { (session, result) in
switch result {
case .failure(let error):
print(error.localizedDescription)
case .success(let response):
// Pass the first generated route to the the NavigationViewController
viewController = NavigationViewController(for: response, routeIndex: 0, routeOptions: routeOptions)
viewController!.modalPresentationStyle = .fullScreen
}
}
viewController = navigationViewController
}
class Coordinator: NSObject, NavigationViewControllerDelegate {
var control: NavigationView
init(_ control: NavigationView) {
self.control = control
}
}
}
'''
这里的问题似乎是它根本没有通过:Directions.shared.calculate()。
这是视频中问题的链接:https ://drive.google.com/file/d/19zHVbWaYjvWkzMD-cKvuXbJ5EMs10k5h/view?usp=sharing
提前谢谢你的帮助!