我想在两个地点之间找到路线。
以下是我的类函数代码:
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)
}
}
}
代表方法:
extension MapView : MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.blue
return renderer
}
}
运行时,它会在控制台中提供以下消息:
_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
但是地图视图上没有输出!什么都没发生!
我正在使用Swift 3.0& Xcode 8.0
答案 0 :(得分:0)
添加此答案只是为了指导任何面临此问题的未来用户。
在对上述主题进行进一步研究后,我发现Apple Maps的Direction API在我的国家,即印度不受支持。这就是我收到这条消息的原因。因此,我转而使用谷歌地图,这在印度提供了很好的支持。