我正在尝试让 Mapbox 的应用程序导航在 SwiftUI 上工作。现在 viewController 返回 nil。我查看了这个问题的其他答案,其中大多数人建议在发生回调的函数中放置另一个完成处理程序。这是代码:
func makeUIViewController(context: Context) -> NavigationViewController {
getRoute { route in
let viewController = NavigationViewController(for: route as! Route)
viewController.modalPresentationStyle = .fullScreen
}
//Use of unresolved identifier viewController
return viewController
}
向此函数添加闭包会给我错误:
类型“NavView”不符合协议“UIViewControllerRepresentable”
除了向函数添加闭包之外,我还能做些什么来从 getRoute{} 中获取值?
这是其余的代码:
struct NavView: UIViewControllerRepresentable{
typealias CompletionHandler = (Any) -> Void
func makeUIViewController(context: Context) -> NavigationViewController {
getRoute { route in
let viewController = NavigationViewController(for: route as! Route)
viewController.modalPresentationStyle = .fullScreen
}
//Use of unresolved identifier viewController
return viewController
}
func updateUIViewController(_ uiView: NavigationViewController, context: Context) {
}
func getRoute(_ completionHandler: @escaping CompletionHandler){
// 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 options = NavigationRouteOptions(waypoints: [origin, destination])
// Request a route using MapboxDirections.swift
Directions.shared.calculate(options) { (waypoints, routes, error) in
guard let route = routes?.first else { return }
completionHandler(route)
}
}