iOS Swift:地图框路线计算不起作用,"Value of type 'Result<RouteResponse, DirectionsError>' has no member



所以我遵循了我在YT "建立一个iOS应用程序的教程:(2/4)使用Mapbox导航SDK"计算路线;当我试图自己跟随时遇到了一个问题。

在创建calculateRoute函数时,xCode抛出了多个错误,特别是在directions .share .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, coordinateAccuracy: -1, 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告诉抛出以下错误:

  1. 上下文闭包类型'(方向。Session, Result<routerresponse,>) ->Void'(即'(options: DirectionsOptions, credentials: DirectionsCredentials), Result<routerresponse,>) ->()')期望2个参数,但闭包体中使用了3个参数一旦我从上面的链接中删除了第三个参数(error),就会出现两个新的错误:2)类型"Result<routerresponse,>"的值没有成员"first"3)不能对'Result<routerresponse,>'类型的非可选值使用可选链接

我似乎修不好,因为我是初学者。我也找不到任何关于它的文件。这行代码应该将directionsRoute设置为返回的路由中的第一个路由。我一步一步地遵循教程,我错过了什么吗?谢谢!

第二个参数(只有两个)是一个Result类型,一个带有关联值的enum。它同时包含routeserror

你必须写

_ = 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)
}  
})

相关内容

  • 没有找到相关文章