Dragging the map after dragging my MKAnnotationView does not move the MKAnnotationView with it
Asked Answered
C

3

8

MKPinAnnotationView does not allow you to use a custom image as 'pin' and enable dragging at the same time, because the image will change back to the default pin as soon as you start dragging. Therefore I use an MKAnnotationView instead of an MKPinAnnotationView.

While using MKAnnotationView instead of MKPinAnnotationView does keep your custom image shown as your 'pin' it doesn't support the drag & drop animation that you get with the default pin.

Anyway, my issue is that after I drag my custom MKAnnotationView to a new point on the map and then move the map itself the MKAnnotationView does not move with the map anymore.

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    static NSString *defaultID = @"myLocation";

    if([self.annotation isKindOfClass:[PinAnnotation class]])
    {
        //Try to get an unused annotation, similar to uitableviewcells
        MKAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:defaultID];

        //If one isn't available, create a new one
        if(!annotationView)
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:self.annotation reuseIdentifier:defaultID];
            annotationView.canShowCallout = YES;
            annotationView.draggable = YES;
            annotationView.enabled = YES;
        }
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 32, 32)];
        imgView.image = self.passableTag.image;
        annotationView.leftCalloutAccessoryView = imgView;
        annotationView.image = [UIImage imageNamed:[Constants tagIconImageNameForTagType:self.passableTag.type]];
        return annotationView;
    }
    return nil;
}
Comradery answered 21/9, 2013 at 14:15 Comment(2)
How many PinAnnotations are you allowing the user to put on a map at any one time?Shurwood
@Shurwood There is only one.Comradery
P
9

I had the same problem and I solved it adding "setDragState" to my MKAnnotationView class.

This is an old solution but it worked to me (iOS8).

Peterec answered 28/9, 2013 at 11:12 Comment(0)
B
15

Just set the annotationView.dragState to MKAnnotationViewDragStateNone after ending the drag:

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {
        if ([view.annotation isKindOfClass:[PinAnnotation class]]) {
            if (newState == MKAnnotationViewDragStateEnding) {
                view.dragState = MKAnnotationViewDragStateNone;
            }
        }
    }
Breakaway answered 7/3, 2014 at 12:4 Comment(1)
This is exactly the fix I needed too - setting the MKAnnotationView's dragState to MKAnnotationViewDragStateNone. It's not obvious that this would be required, because since this is a delegate method you would expect that the calling code that was dealing with the state change would be updating this property. Seems to be an Apple bug introduced in iOS 7, because I could not repro on iOS 6.Tecu
P
9

I had the same problem and I solved it adding "setDragState" to my MKAnnotationView class.

This is an old solution but it worked to me (iOS8).

Peterec answered 28/9, 2013 at 11:12 Comment(0)
B
-2

Don't reuse MKAnnotationView and it will work.

Bolitho answered 26/9, 2013 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.