I am trying to get route between two locations.
Following is my code for class function:
func fetchRoute(index: Int){
self.resultView.isHidden = true;
destLocation = self.arrResult[index];
let request: MKDirectionsRequest = MKDirectionsRequest()
request.source = curLocation
request.destination = destLocation
request.transportType = MKDirectionsTransportType.automobile;
request.requestsAlternateRoutes = true
let directions = MKDirections(request: request)
directions.calculate { [unowned self] response, error in
guard let unwrappedResponse = response else { return }
for route in unwrappedResponse.routes {
self.mapView.add(route.polyline)
self.mapView.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
}
}
}
And for delegate Method:
extension MapView : MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.blue
return renderer
}
}
When run, it give following messages in console:
_errorCodeForProblemDetail: unexpected waypoint index
nw_socket_handle_socket_event Event mask: 0x4
nw_socket_handle_socket_event Socket received WRITE_CLOSE event
tcp_connection_cancel 1
nw_endpoint_handler_cancel [1 gsp-ssl.ls.apple.com:443 ready resolver (satisfied)]
nw_endpoint_handler_cancel [1.1 17.130.137.75:443 cancelled socket-flow (null)]
nw_endpoint_handler_cancel [1.2 17.130.137.77:443 ready socket-flow (satisfied)]
__nw_socket_service_writes_block_invoke sendmsg(fd 9, 37 bytes): socket has been closed
cancelled socket-flow (null)] Socket protocol sent error: [32] Broken pipe
nw_endpoint_flow_protocol_disconnected [1.2 17.130.137.77:443 cancelled socket-flow (null)] Output protocol disconnected
nw_endpoint_handler_cancel [1.3 17.130.137.73:443 initial path (null)]
nw_endpoint_handler_cancel [1.4 17.130.137.79:443 initial path (null)]
nw_resolver_cancel_on_queue 0x7d9fa730
[NWConcrete_tcp_connection dealloc] 1
But there is no output on map view! Nothing happens!
I am using Swift 3.0 & Xcode 8.0
Adding this answer just to guide any future users facing this issue.
After further research on above topic I found out that Direction API of Apple Maps is not supported in my country, i.e. India. That was the reason I was getting this message. Thus I switched to Google Maps, which provide a good support in India.