Pregunta

I have made this implemention of MKAnnotionView:

- (MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    NSString *identifier = @"mypin";

    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if(annotationView == nil)
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationView.canShowCallout = YES;
        UIImageView *pin = [[UIImageView alloc] initWithImage:[UIImage imageNamed:identifier]];
        UIImageView *shadow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin_shadow"]];
        shadow.frame = CGRectMake(21, 36, 60, 27);

        [annotationView addSubview:shadow];
        [annotationView addSubview:pin];

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 11, 20);
        [btn setImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];

        annotationView.rightCalloutAccessoryView = btn;
        annotationView.centerOffset = CGPointMake(-(44.0f/2), -62.0f);
    }

    return annotationView;
}

The pins and shadows show up fine on the map but when i press a pin the callout is now shown. I am positive that the annotation objects have a title and a subtitle value.

If i add my pin using the .image property on the MKAnnotionView it works but then my shadow is on top of the pin.. mehh! :/

What is going wrong?

¿Fue útil?

Solución

Since you are adding your image as a subview of the annotationView and not using the image setter, I believe the frame of your annotationView is CGRectZero. (You can easily check that by activating clipToBounds to true) Therefore there is no hitZone for your pin to receive the event and show the callout. You may want to set the bounds of your annotationView to the total size of both your images (pin + shadow).

annotationView.bounds = CGRectMake(0, 0, pin.frame.size.width, pin.frame.size.width);
//+Shadow ? if not : annotationView.bounds = pin.bounds;

On an unrelated note, I believe you may experience an issue when reusing annotations, as you are not resetting the annotation to the annotationView. You may want to change it to :

if(annotationView ==nil) { }
annotationView.annotation = annotation;
return annotationView;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top