问题描述
因此,我正在学习我在 YT 上找到的教程“构建 iOS 应用程序:(2/4) 使用 MapBox 导航 SDK 计算路线”,但在尝试自行操作时遇到了问题。
不知何故,xCode 在创建 calculateRoute 函数时抛出了多个错误,尤其是在 Directions.shared.calculate(..) 中。这是我的代码:
import UIKit
import MapBox
import MapBoxNavigation
import MapBoxDirections
import MapboxcoreNavigation
class ThirdViewController: UIViewController,MGLMapViewDelegate{
//is the View that shows the map
var mapView: NavigationMapView!
//Saves the route
var directionsRoute: Route?
//creates the button
var navigateButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//view.backgroundColor = UIColor(red: 0.19,green: 0.21,blue: 0.24,alpha: 1.00)
mapView = NavigationMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
view.addSubview(mapView)
mapView.delegate = self
mapView.showsUserLocation = true
mapView.setUserTrackingMode(.follow,animated: true)
addButton()
}
func addButton(){
navigateButton = UIButton(frame: CGRect(x: (view.frame.width/2) - 100,y: view.frame.height - 200,width: 200,height: 50))
navigateButton.backgroundColor = UIColor.white
navigateButton.setTitle("Route finden",for: .normal)
navigateButton.setTitleColor(UIColor(red: 59/255,green: 178/255,blue: 208/255,alpha: 1),for: .normal)
navigateButton.titleLabel?.font = UIFont(name: "AvenirNext-Demibold",size: 18)
navigateButton.layer.cornerRadius = 25
navigateButton.layer.shadowOffset = CGSize(width: 0,height: 10)
//navigateButton.layer.shadowColor = C
navigateButton.layer.shadowRadius = 5
navigateButton.layer.shadowOpacity = 0.3
navigateButton.addTarget(self,action: #selector(navigateButtonWaspressed(_:)),for: .touchUpInside)
view.addSubview(navigateButton)
}
@objc func navigateButtonWaspressed(_ sender: UIButton){
}
func calculateRoute(from originCoor: CLLocationCoordinate2D,to destinationCoor: CLLocationCoordinate2D,completion: @escaping (Route?,Error?) -> Void ){
let origin = Waypoint(coordinate: originCoor,coordinateAccuracy: -1,name: "Start")
let destination = Waypoint(coordinate: destinationCoor,name: "Finish")
let options = NavigationRouteOptions(waypoints: [origin,destination],profileIdentifier: .automobileAvoidingTraffic)
_ = Directions.shared.calculate(options,completionHandler: { (wayponts,routes,error) in
self.directionsRoute = routes?.first //HERE IS THE ERROR
let coordinateBounds = MGLCoordinateBounds(sw: destinationCoor,ne: originCoor)
})
}
}
我已经标记了导致问题的那一行。 xCode 告诉抛出以下错误:
- 上下文闭包类型'(Directions.Session,Result
) -> Void' (又名'((options: DirectionsOptions,credentials: DirectionsCredentials),Result ) -> ()')需要 2 个参数,但在闭包体中使用了 3 个 一旦我从上面的 lign 中删除第三个参数(错误),就会出现两个新错误: 2) 'Result ' 类型的值没有成员 'first' 3) 不能对 'Result ' 类型的非可选值使用可选链
我似乎无法修复它,因为我是初学者。我也找不到任何关于它的文档。 这行代码应该将 DirectionsRoute 设置为返回的路线中的第一条路线。我一步一步地跟着教程,我错过了什么吗? 谢谢!
解决方法
第二个参数——只有两个——是一个 Result
类型,一个带有关联值的枚举。它包含 routes
和 error
你必须写
_ = Directions.shared.calculate(options,completionHandler: { (waypoints,result) in
switch result {
case .success(let response):
guard let route = response.routes?.first else { return }
self.directionsRoute = route
let coordinateBounds = MGLCoordinateBounds(sw: destinationCoor,ne: originCoor)
case .failure(let error): print(error)
}
})