Custom annotation pin changes to default red pin at long tap
Asked Answered
B

1

8

I have custom annotation pin at app:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    return [kml viewForAnnotation:annotation type:state];
}

where I return custom view and make setImage for annotationView of Placemark such as:

- (MKAnnotationView *)viewForAnnotation:(id <MKAnnotation>)point type:(int)state
{
    // Find the KMLPlacemark object that owns this point and get
    // the view from it.
    for (KMLPlacemark *placemark in _placemarks) {
        if ([placemark point] == point) 
        {
            UIButton *disclosureButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure]; 
            [[placemark annotationView] setCanShowCallout: YES];            
            [[placemark annotationView] setRightCalloutAccessoryView:disclosureButton];

            if (state == 0)
            {
                [[placemark annotationView] setImage:[UIImage imageNamed:@"ic_pin_tour.png"]];
            }
            else
            {
                [[placemark annotationView] setImage:[UIImage imageNamed:@"ic_pin_point.png"]];
            }

            return [placemark annotationView];
        }
    }
    return nil;
}

but if I long tap at my annotation pin it changes appearance to its default view (RedPin). I cannot understand what method is called on long tap. I tried to play with UITapGestureRecognizer, but did not find out. If I just tap annotation pin all works fine and my custom annotation pin view doesn't disappear. You can see what I mean in this screenshot: so useful image with example

So, why annotation pin appearance changes on long tap?

Broads answered 24/11, 2011 at 12:39 Comment(0)
B
23

So, if you want to use a custom image for an annotation view, always use a generic MKAnnotationView instead of an MKPinAnnotationView. I have MKPinAnnotationView at just one place, when I replace it with MKAnnotationView everything works properly now:

- (MKAnnotationView *)annotationView
{
    if (!annotationView) {
        id <MKAnnotation> annotation = [self point];
        if (annotation) {
            MKAnnotationView *pin =
                [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
            pin.canShowCallout = YES;
            annotationView = pin;
        }
    }
    return annotationView;
}
Broads answered 24/11, 2011 at 13:19 Comment(1)
Worked as charm. The fact is, that MKPinAnnotation view is a subclass with MKAnnotation view with functionality you don't want.Manslaughter

© 2022 - 2024 — McMap. All rights reserved.