how to manage drag and drop for MKAnnotationView on IOS?

Viewed 15704

I'm working without InterfaceBuilder.

I've an instance of MKAnnotationView with setDraggable on YES, In My MKMapView my annotation view is displayed and I can drag and drop it.

How can I execute an method when the drop action is performed? In this method I need the new coordonates of my annotation view.

3 Answers

Swift solution :

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState)
    {
        if (newState == MKAnnotationViewDragState.ending)
        {
            let droppedAt = view.annotation?.coordinate
            print("dropped at : ", droppedAt?.latitude ?? 0.0, droppedAt?.longitude ?? 0.0);
            view.setDragState(.none, animated: true)    
        }
        if (newState == .canceling )
        {
            view.setDragState(.none, animated: true)           
        }
    }
Related