【发布时间】:2017-10-28 10:49:19
【问题描述】:
我想绘制一条从当前用户位置到注释点的折线,但它似乎没有绘制任何东西:
@IBAction func myButtonGo(_ sender: Any) {
showRouteOnMap()
}
func showRouteOnMap() {
let request = MKDirectionsRequest()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D.init(), addressDictionary: nil))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: (annotationCoordinatePin?.coordinate)!, addressDictionary: nil))
request.requestsAlternateRoutes = true
request.transportType = .automobile
let directions = MKDirections(request: request)
directions.calculate { [unowned self] response, error in guard let unwrappedResponse = response else { return }
if (unwrappedResponse.routes.count > 0) {
self.mapView.add(unwrappedResponse.routes[0].polyline)
self.mapView.setVisibleMapRect(unwrappedResponse.routes[0].polyline.boundingMapRect, animated: true)
}
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.black
return renderer
}
我尝试在调试模式下运行,它在以下行的断点处停止:
directions.calculate { [unowned self] response, error in guard let unwrappedResponse = response else { return }
这个错误的原因是什么?
【问题讨论】:
标签: swift xcode mapkit draw polyline