如何使注释在折线上移动

时间:2019-04-23 19:16:37

标签: swift mapkit direction

我有3条注释,并且在第一条注释和第二条注释之间绘制了一条折线,但是我需要在该条折线上移动第三个,但是它总是沿街道折线移动到目标位置

-我的代码

   func moveDelivery(_ destinationCoordinate : CLLocationCoordinate2D{
        self.deliveryAnnotation.coordinate =      CLLocationCoordinate2DMake(29.959640, 31.270421)
    let sourcePlaceMark = MKPlacemark(coordinate:     self.userAnnotation.coordinate)
    //sourcePlaceMark.title
    let destPlaceMkark = MKPlacemark(coordinate:   self.deliveryAnnotation.coordinate)

    let sourceItem = MKMapItem(placemark: sourcePlaceMark)
    let destItem = MKMapItem(placemark: destPlaceMkark)

    let directionRequest = MKDirections.Request()
    directionRequest.source = sourceItem
    directionRequest.destination = destItem
    directionRequest.transportType = .any

    let direction = MKDirections(request: directionRequest)
    direction.calculate(completionHandler: {
        response, error in
        guard let response = response else {

            if let error = error {
                print(error.localizedDescription)
            } else {
                self.deliveryAnnotation.courseDegrees =   self.getHeadingForDirectionFromCoordinate(self.kitchenAnnotation.coordinate, toLoc: self.userAnnotation.coordinate)
                self.view.transform = CGAffineTransform(rotationAngle:CGFloat(self.deliveryAnnotation.courseDegrees))
            }
            return
        }
        guard let primaryRoute = response.routes.first else { return }
        let route = response.routes[0]
        self.mapView.addOverlay(route.polyline, level: .aboveRoads)

        let rekt = route.polyline.boundingMapRect
        self.mapView.setRegion(MKCoordinateRegion(rekt), animated: true)

    })

    //
    UIView.animate(withDuration: Double(60), animations: {

        self.deliveryAnnotation.coordinate = destinationCoordinate

    }, completion:  { success in
        if success {

        }
    })
}

ScreenShot

1 个答案:

答案 0 :(得分:1)

您的第三个注释未遵循该路线,因为您正在对其进行动画处理,使其沿第一行和第二行之间的直线移动。尝试从MKRoute的折线获取坐标,并在每个折线之间设置动画(根据苹果的文档,MKRoutes由坐标组成,但您也可以使用点)

如果您希望它在60秒的时间内进行动画处理:

func moveDelivery(_ destinationCoordinate: CLLocationCoordinate2D) {

    // I don't know why you have the delivery annotation start here, is this for testing?
    deliveryAnnotation.coordinate = CLLocationCoordinate2DMake(29.959640, 31.270421)

    let sourcePlaceMark = MKPlacemark(coordinate: destinationCoordinate)
    let destPlaceMkark = MKPlacemark(coordinate: userAnnotation.coordinate)

    let directionRequest = MKDirections.Request()
    directionRequest.source = MKMapItem(placemark: sourcePlaceMark)
    directionRequest.destination = MKMapItem(placemark: destPlaceMkark)
    directionRequest.transportType = .any
    let direction = MKDirections(request: directionRequest)

    direction.calculate(completionHandler: {
        response, error in
        guard let response = response else {
            print("MKRequest gave no response")
            if let error = error {
                print(error.localizedDescription)
            } else {
                self.deliveryAnnotation.courseDegrees =   self.getHeadingForDirectionFromCoordinate(self.kitchenAnnotation.coordinate, toLoc: self.userAnnotation.coordinate)
                self.view.transform = CGAffineTransform(rotationAngle:CGFloat(self.deliveryAnnotation.courseDegrees))
            }

            return
        }

        guard let primaryRoute = response.routes.first else {
            print("response has no routes")
            return
        }

        self.mapView.addOverlay(primaryRoute.polyline, level: .aboveRoads)

        let rekt = primaryRoute.polyline.boundingMapRect
        self.mapView.setRegion(MKCoordinateRegion(rekt), animated: true)

        let coordinateArray = primaryRoute.polyline.coordinates

        assert(coordinateArray.count > 0, "coordinate array is empty")

        self.routeCoordinates = coordinateArray

        // initiate recursive animations
        self.coordinateIndex = 0
    })

}

var routeCoordinates = [CLLocationCoordinate2D]()

var avgAnimationTime: Double {
    return 60 / Double(routeCoordinates.count)
}

var coordinateIndex: Int! {
    didSet {
        guard coordinateIndex != routeCoordinates.count else {
            print("animated through all coordinates, stopping function")
            return
        }

        animateToNextCoordinate()
    }
}

func animateToNextCoordinate() {
    let coordinate = routeCoordinates[coordinateIndex]

    UIView.animate(withDuration: avgAnimationTime, animations: {
        self.deliveryAnnotation.coordinate = coordinate
    }, completion:  { _ in
        self.coordinateIndex += 1
        print("moved between coordinates")
    })
}

编辑

请确保包括此扩展名,否则您将无法获取MKRoute的坐标(来源:https://gist.github.com/freak4pc/98c813d8adb8feb8aee3a11d2da1373f

public extension MKMultiPoint {
    var coordinates: [CLLocationCoordinate2D] {
        var coords = [CLLocationCoordinate2D](repeating: kCLLocationCoordinate2DInvalid,
                                              count: pointCount)

        getCoordinates(&coords, range: NSRange(location: 0, length: pointCount))

        return coords
    }
}

编辑#2

请参见上文,编辑原始答案以在上一个动画结束后通过每个坐标进行动画处理。真的很粗糙,但是应该可以。

编辑#3

添加了代码以获取destination变量以及一些assert和debug打印调用。如果这次没有任何效果,请告诉我您收到哪些调试消息。

编辑#4

我只是演示了我的代码,它可以工作。这是我使用的MapViewController类以及必要的扩展名:

Demo_gif

private let reuseId = "deliveryReuseId"

private let userTitle = "user"
private let startingPointTitle = "store"
private let deliveryTitle = "delivery truck"

class MapViewController: UIViewController {

    var mapView: MKMapView!

    // annotations for this demo, replace with your own annotations
    var deliveryAnnotation: MKPointAnnotation = {
        let annotation = MKPointAnnotation()
        annotation.title = deliveryTitle

        return annotation
    }()

    let userAnnotation: MKPointAnnotation = {
        let annotation = MKPointAnnotation()
        annotation.title = userTitle
        annotation.coordinate = CLLocationCoordinate2DMake(29.956694, 31.276854)
        return annotation
    }()

    let startingPointAnnotation: MKPointAnnotation = {
        let annotation = MKPointAnnotation()
        annotation.title = startingPointTitle
        annotation.coordinate = CLLocationCoordinate2DMake(29.959622, 31.270363)
        return annotation
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        loadMapView()
        navigate()
    }

    func loadMapView() {
        // set map
        mapView = MKMapView()
        view = mapView
        mapView.delegate = self
        mapView.register(MKAnnotationView.self, forAnnotationViewWithReuseIdentifier: reuseId)

        // add annotations
        mapView.addAnnotation(userAnnotation)
        mapView.addAnnotation(startingPointAnnotation)
        mapView.addAnnotation(deliveryAnnotation)
    }

    func navigate() {
        let sourcePlaceMark = MKPlacemark(coordinate: startingPointAnnotation.coordinate)
        let destPlaceMkark = MKPlacemark(coordinate: userAnnotation.coordinate)

        let directionRequest = MKDirections.Request()
        directionRequest.source = MKMapItem(placemark: sourcePlaceMark)
        directionRequest.destination = MKMapItem(placemark: destPlaceMkark)
        directionRequest.transportType = .any

        let direction = MKDirections(request: directionRequest)

        direction.calculate(completionHandler: { response, error in
            if let error = error {
                print(error.localizedDescription)

                return
            }

            guard let primaryRoute = response!.routes.first else {
                print("response has no routes")
                return
            }

            self.mapView.addOverlay(primaryRoute.polyline, level: .aboveRoads)
            self.mapView.setRegion(MKCoordinateRegion(primaryRoute.polyline.boundingMapRect), animated: true)

            // initiate recursive animation
            self.routeCoordinates = primaryRoute.polyline.coordinates
            self.coordinateIndex = 0
        })
    }

    var routeCoordinates = [CLLocationCoordinate2D]()

    var avgAnimationTime: Double {
        // to show delivery in 60 second, replace 60 with amount of seconds you'd like to show
        return 60 / Double(routeCoordinates.count)
    }

    var coordinateIndex: Int! {
        didSet {
            guard coordinateIndex != routeCoordinates.count else {
                print("animated through all coordinates, stopping function")
                return
            }

            animateToNextCoordinate()
        }
    }

    func animateToNextCoordinate() {
        let coordinate = routeCoordinates[coordinateIndex]

        UIView.animate(withDuration: avgAnimationTime, animations: {
            self.deliveryAnnotation.coordinate = coordinate
        }, completion:  { _ in
            self.coordinateIndex += 1
        })
    }
}

extension MapViewController: MKMapViewDelegate {

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)

        // replace these images with your own
        switch annotation.title {
        case userTitle:
            annotationView.image = UIImage(named: "user")
        case startingPointTitle:
            annotationView.image = UIImage(named: "store")
        case deliveryTitle:
            annotationView.image = UIImage(named: "deliveryTruck")
        default: break
        }

        return annotationView
    }

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {

        guard overlay is MKPolyline else {
            return MKOverlayRenderer()
        }

        let renderer = MKPolylineRenderer(overlay: overlay)
        renderer.strokeColor = .black
        renderer.lineWidth = 5
        renderer.lineJoin = .round

        return renderer
    }
}

public extension MKMultiPoint {
    var coordinates: [CLLocationCoordinate2D] {
        var coords = [CLLocationCoordinate2D](repeating: kCLLocationCoordinate2DInvalid,
                                              count: pointCount)

        getCoordinates(&coords, range: NSRange(location: 0, length: pointCount))

        return coords
    }
}