There are lots of examples about how to use the delegate method mapView:viewForAnnotation:
that show setting up an MKAnnotationView
. But what is not as obvious is that just because you set the draggable
property of your MKAnnotationView
instance to YES
, you still have to write some code to help it transition some of the states. MapKit will take care of moving your instance's dragState
to MKAnnotationViewDragStateStarting
and MKAnnotationViewDragStateEnding
, but it will not do the other transitions. You see hints of this in the docs notes about subclassing MKAnnotationView
and the need to override the `setDragState:animated:'
When the drag state changes to MKAnnotationViewDragStateStarting, set the state to MKAnnotationViewDragStateDragging. If you perform an animation to indicate the beginning of a drag, and the animated parameter is YES, perform that animation before changing the state.
When the state changes to either MKAnnotationViewDragStateCanceling or MKAnnotationViewDragStateEnding, set the state to MKAnnotationViewDragStateNone. If you perform an animation at the end of a drag, and the animated parameter is YES, you should perform that animation before changing the state.
In this case, I'm not subclassing, but it seems that MKAnnotationView
still struggles to make the transitions on its own. So you have to implement the delegate's mapView:annotationView:didChangeDragState:fromOldState:
method. E.g.
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState {
if (newState == MKAnnotationViewDragStateStarting) {
annotationView.dragState = MKAnnotationViewDragStateDragging;
}
else if (newState == MKAnnotationViewDragStateEnding || newState == MKAnnotationViewDragStateCanceling) {
annotationView.dragState = MKAnnotationViewDragStateNone;}
}
}
This allows things to complete appropriately, so that when you pan the map after dragging the annotation, the annotation moves with the pan.