I want to draw a polyline from the current user location to an annotation point but it doesn't appear to draw anything:
@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
}
I tried to run in debug mode and it stops on the break point at line of:
directions.calculate { [unowned self] response, error in guard let unwrappedResponse = response else { return }
What is the cause of this error?