0

我有这段代码来获取从源点(userLocation)到注释(从用户中选择的引脚)的方向。`

    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)。我能怎么做?

4

1 回答 1

1

这就是从折线获取坐标的方式。尽管您只需要将折线添加到地图中即可。需要坐标是不寻常的。

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)
        }
    }
}
于 2017-05-30T22:34:25.950 回答