callback when dragging annotation

annotations, drag-and-drop, ios

Solution

You can set an observer on the MKAnnotationView center property. Then in the callback for the observer, you can convert the annotation view center location from screen coordinates to geo coordinates.

[myAnnotationView addObserver:myMapViewDelegate forKeyPath:@"center" options:NSKeyValueObservingOptionNew context:nil];
...
- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context
{
    CGPoint position = myAnnotationView.center;
    //... here take  myAnnotationView.centerOffset into consideration to get the correct coordinate  
    CLLocationCoordinate2D newCoordinate = [self.mapView convertPoint:position toCoordinateFromView:self.superview];
}

Problem

I can drag my annotation and when I drop it I can read the position. However, I need to constantly update my title with the position I'm dragging. I've tried with adding a UIPanGestureRecognizer but that doesn't work when I dragging the annotation. What method shall I use to get constantly calls back to my code while dragging an annotation? ``` -(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState { CLLocationCoordinate2D annoCoord = view.annotation.coordinate; switch (newState) { case MKAnnotationViewDragStateStarting: NSLog(@"Start dragging annotation"); break; case MKAnnotationViewDragStateDragging: NSLog(@"Dragging annotation"); break; case MKAnnotationViewDragStateEnding: [view setDragState:MKAnnotationViewDragStateNone]; // must be here! else multiple calls NSLog(@"Ending dragging annotation at %f : %f", annoCoord.latitude, annoCoord.longitude); break; case MKAnnotationViewDragStateCanceling: NSLog(@"Cancel dragging annotation"); break; case MKAnnotationViewDragStateNone: NSLog(@"None dragging annotation"); break; default: break; } } ``` I only get callbacks when a state starts, not during the dragging. Any help would be much appreciated. cheers, tord

Original source