我正在尝试创建一个应用程序,在该应用程序中,用户可以在视图控制器中选择他想要查找路线的位置,之后,将执行segue并打开MapView他需要执行的方向。这是地图视图和路线请求的代码:
mapkitView.delegate = self
mapkitView.showsScale = true
mapkitView.showsPointsOfInterest = true
mapkitView.showsUserLocation = true
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
let sourceCoordinates = locationManager.location?.coordinate
var destCoordinates = CLLocationCoordinate2D(latitude: 36.0922147,longitude: -113.4035741)
let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates!)
let destPlacemark = MKPlacemark(coordinate: destCoordinates)
let sourceItem = MKMapItem(placemark: sourcePlacemark)
let destItem = MKMapItem(placemark: destPlacemark)
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceItem
directionRequest.destination = destItem
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate(completionHandler: {
response, error in
guard let response = response else {
if let error = error {
print("Something went wrong.")
}
return
}
let route = response.routes[0]
self.mapkitView.add(route.polyline, level: .aboveRoads)
let rekt = route.polyline.boundingMapRect
self.mapkitView.setRegion(MKCoordinateRegionForMapRect(rekt), animated: true)
})
位置管理器定义为CLLocationManager()
。目的地坐标默认设置为大峡谷国家公园,此代码位于override func viewDidLoad()
内。
在另一个视图控制器中,用户将选择他想要的地方作为最终目的地,然后我想要将地图执行到目的地将更改为用户想要的地图,然后指示将也改变了。我试图这样做,但它不起作用:
self?.performSegue(withIdentifier: "teste", sender: self)
self?.firstViewController.destCoordinates = CLLocationCoordinate2D(latitude: -23.5957034,longitude: -46.675363)
有人可以帮助我吗?
答案 0 :(得分:0)
您可以使用所需的参数来实例化下一个VC,而不是使用segue吗?
let storyboard = UIStoryboard(name: "Storyboard", bundle: nil)
let nextViewcontroller = self.storyboard!.instantiateViewController(withIdentifier: "nextViewcontroller") as! NextViewcontroller
nextViewcontroller.destCoordinates = YOUR LOCATION
当然你需要在下一个VC中创建一个参数:
var destCoordinates : CLLocation?
// or
var destCoordinates : CLLocation!
如果您决定在下一个VC中创建一个可选项,则需要使用if let
如果您决定强制解包您的变量,我建议您在viewDidLoad中使用precondition(destCoordinates != nil)
来确保您执行此操作设置你的变量