如何在Swift中使用MapKit在两个位置之间绘制路线?

如何在Swift中使用MapKit在用户的当前位置与特定位置之间绘制路线

我search了很多,但没有find任何有用的Swift特定的链接或教程。

我只是链接到一个教程,显示如何在Swift中做到这一点:

Ray Wenderlich:在MapKit中覆盖

这应该涵盖你所需要的。

class MapController: UIViewController, MKMapViewDelegate { func showRouteOnMap() { let request = MKDirectionsRequest() request.source = MKMapItem(placemark: MKPlacemark(coordinate: annotation1.coordinate, addressDictionary: nil)) request.destination = MKMapItem(placemark: MKPlacemark(coordinate: annotation2.coordinate, addressDictionary: nil)) request.requestsAlternateRoutes = true request.transportType = .Automobile let directions = MKDirections(request: request) directions.calculateDirectionsWithCompletionHandler { [unowned self] response, error in guard let unwrappedResponse = response else { return } if (unwrappedResponse.routes.count > 0) { self.mapView.addOverlay(unwrappedResponse.routes[0].polyline) self.mapView.setVisibleMapRect(unwrappedResponse.routes[0].polyline.boundingMapRect, animated: true) } } } func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { if overlay is MKPolyline { var polylineRenderer = MKPolylineRenderer(overlay: overlay) polylineRenderer.strokeColor = UIColor.blueColor() polylineRenderer.lineWidth = 5 return polylineRenderer } return nil } 

返回是一组可能的路线,通常我们只是想显示第一个。 注释是地图注释。