我成功地生成了一个通用的 MGLMapView 和带有生成路线的折线叠加层,但是在为我的 Turn-by-Turn Directions 功能构建 NavigationViewController 时遇到了障碍。我编译并运行以下代码:
struct MapboxNavigationView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> NavigationViewController {
var viewController: NavigationViewController
let directions = Directions(credentials: DirectionsCredentials(accessToken: "***PrivateKey***", host: nil))
let waypoints2 = [
Waypoint(coordinate: CLLocationCoordinate2D(latitude: 32.623251, longitude: -117.134877), coordinateAccuracy: 1, name: "Origin"),
Waypoint(coordinate: CLLocationCoordinate2D(latitude: 32.672401, longitude: -117.166996), coordinateAccuracy: 1, name: "Destination"),
]
var route = Route?
let options = RouteOptions(waypoints: waypoints2, profileIdentifier: .automobile)
options.includesSteps = true
directions.calculate(options) { (session, result) in
switch result {
case .failure(let error):
print("error = \(error.localizedDescription)")
case .success(let response):
route = response.routes?.first
}
}
viewController = NavigationViewController(for: route, routeOptions: options)
viewController.modalPresentationStyle = .fullScreen
return viewController
}
func updateUIViewController(_ uiViewController: NavigationViewController, context: Context) {
}
}
我在 RouteProgress.swift (MapboxCoreNavigation CocoaPods) 的第 183 行(下方)收到一条错误消息,“线程 1:EXC_BAD_ACCESS(代码=1,地址=0x77797ced916870)”:
self.currentLegProgress = RouteLegProgress(leg: route.legs[legIndex], stepIndex: 0, spokenInstructionIndex: spokenInstructionIndex)
我怀疑 NavigationController 需要访问用户在路线中所在的当前支路。任何帮助将不胜感激!