2

Currently I am implementing a feature, where the user can correct manually a pin on the map. I have set the annotationView to annotationView.draggable = YES; and I have also implemented the delegate method to receive the new coordinates. But how can I now tell the mapView that the dragging action should stop now and the pin gets its fixed position on the map, even if the map is then moved.

The current behaviour is that after the dragging of the pin to a new position, if I then move the map, the pin has its fixed position just on the device screen but not on the map.

Help is appreciated :).

Delegate:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState
{
    if (newState == MKAnnotationViewDragStateEnding)
    {
        CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
        NSLog(@"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
    }
}
  • you could try setting the annotationView.draggable = NO; inside the if statement – user2277872 Feb 27 '14 at 15:53
  • How are you updating the annotation coordinates? In the code you provided you do not do anything with the new coordinates. – Dean Davids Feb 27 '14 at 16:07
  • @DeanDavids You don't have to update the annotation coordinates. The act of dragging does that for you. (Clearly, if you want to update your model, then do so, but the annotation itself is updated for you.) – Rob Feb 27 '14 at 16:52

1 Answers1

4

UPDATE:

ok, fixed it:

- (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;
    }
}

Now I just have to persist the new coordinates and I am fine.. - maybe I will add some custom animations for the states..:

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.

  • +1 Just to clarify, this looks like behavior unique to iOS 7 when you do not use `MKPinAnnotationView`. For pin annotation views, or for earlier versions of iOS, this appears to be unnecessary. But this is a good find. – Rob Feb 27 '14 at 16:57