Xcode 方向坐标 Swift

Xcode Directions coordinate in Swift

我有这段代码可以获取从源点(用户位置)到注释(从用户选择的引脚)的方向。`

    func showRouteOnMap() {
    let request = MKDirectionsRequest()

    request.source = MKMapItem(placemark: MKPlacemark(coordinate: (userLocationForDirections?.coordinate)!, addressDictionary: nil))
    if(annotationPin != nil){
    request.destination = MKMapItem(placemark: MKPlacemark(coordinate:  (coordinateAnnotationPin)!, addressDictionary: nil))
    }
    request.requestsAlternateRoutes = true
    request.transportType = .walking

    let directions = MKDirections(request: request)

    directions.calculate(completionHandler: {(response, error) in

        if error != nil {
            print("Error getting directions")

        } else {
            self.showRoute(response!)
        }
    })
}

现在我需要获取方向路线的坐标(CLLocationCordinate2D)。我该怎么办?

这是从多段线获取坐标的方法。虽然您应该只需要将折线添加到地图中。要求坐标是不寻常的。

let directions = MKDirections(request: request)

directions.calculate { (response, error) in

    if let error = error {

        print(error.localizedDescription)

        return
    }

    if let response = response {

        if let route = response.routes.first {

            self.map.add(route.polyline, level: .aboveRoads)

            let coordinates: Array<CLLocationCoordinate2D> = {

                var array = Array<CLLocationCoordinate2D>()

                let polyline = route.polyline

                for i in 0 ..< polyline.pointCount {

                    let point = polyline.points()[i]

                    array.append(MKCoordinateForMapPoint(point))
                }

                return array
            }()

            dump(coordinates)
        }
    }
}