点击MKAnnotationView不会调用didSelectAnnotationView委托

时间:2012-05-25 08:28:24

标签: ios mkmapview mkannotationview

看看这段代码,看看你是否可以帮我修复它:

- (void)viewDidLoad
{
//Initialize the array and add annotations for the location on the map

annotations = [[NSMutableArray alloc] init];    
CustomAnnotation *annotation;
CLLocationCoordinate2D coordinate;

//1
coordinate.latitude = 25.220390105797264;
coordinate.longitude = 55.28095199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = @"123";

[annotations addObject:annotation];

//2
coordinate.latitude = 25.218880105797264;
coordinate.longitude = 55.27992199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = @"117";

[annotations addObject:annotation]; 

//3
coordinate.latitude = 25.219280105797264;
coordinate.longitude = 55.28052199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = @"118";

[annotations addObject:annotation]; 

//4
coordinate.latitude = 25.219680105797264;
coordinate.longitude = 55.28052199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = @"119";

[annotations addObject:annotation]; 

//5
coordinate.latitude = 25.219980105797264;
coordinate.longitude = 55.28095199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = @"120";

[annotations addObject:annotation]; 

//6
coordinate.latitude = 25.220380105797264;
coordinate.longitude = 55.28115199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = @"121";

[annotations addObject:annotation];

//7
coordinate.latitude = 25.220350105797264;
coordinate.longitude = 55.28105199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = @"122";

[annotations addObject:annotation];

//8
coordinate.latitude = 25.215050105797264;
coordinate.longitude = 55.28105199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = @"124";

[annotations addObject:annotation];

//9
coordinate.latitude = -35.44138;
coordinate.longitude = -71.66208;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = @"125";

[annotations addObject:annotation]; 

//10
coordinate.latitude = -35.44138;
coordinate.longitude = -71.66208;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = @"126";

[annotations addObject:annotation];
//Set the region and zoom factor of the map view
MKCoordinateRegion region;

region.span.latitudeDelta = 0.01f;
region.span.longitudeDelta = 0.01f;
region.center.latitude = 25.216050105797264;
    region.center.longitude = 55.27905199798584;
    [sheikhZayedRoadMapView addAnnotations:annotations];
[sheikhZayedRoadMapView setRegion:region];
//[siteMapView.layer setCornerRadius:5.0];
}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:
(id <MKAnnotation>)annotation 
{
NSLog(@"Annotation Class: %@", [annotation class]);

static NSString * const kPinAnnotationIdentifier = @"PinIdentifier";
MKAnnotationView *annotationView;
Myriadproregular *annotationIndexLabel;
 //CustomAnnotation *customAnnotation =nil
annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:kPinAnnotationIdentifier];
if (!annotationView) {

    annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kPinAnnotationIdentifier];
        annotationIndexLabel = [[Myriadproregular alloc] initWithFrame:CGRectMake(0, 3, 25 , 20)];
        annotationIndexLabel.font = [UIFont boldSystemFontOfSize:12];
        [annotationIndexLabel setBackgroundColor:[UIColor clearColor]];
        [annotationIndexLabel setTextColor:[UIColor whiteColor]];
        [annotationIndexLabel setTextAlignment:UITextAlignmentCenter];
        [annotationView addSubview:annotationIndexLabel];
        annotationView.canShowCallout = YES;
    annotationView.calloutOffset = CGPointMake(-5, 5);

}else {

    //Get the label if exists
    for (UIView *labelView in annotationView.subviews) {
        if ([labelView isKindOfClass:[UILabel class]]) {
            annotationIndexLabel = (Myriadproregular *) labelView;
            break;
        }
    }
}

if ([annotation isKindOfClass:[MKUserLocation class]]) {

    [annotationView setImage:[UIImage imageNamed:@"map_notify_bg.png"]];

}else {
    CustomAnnotation *customAnnotation = (CustomAnnotation *) annotation;
    [annotationIndexLabel setText:customAnnotation.annotationIndex];

    [annotationView setImage:[UIImage imageNamed:@"ico_inspector.png"]];
}

return annotationView;
}

问题是:

当我点击提供给didSelectAnnotationView的自定义图片时,我没有调用MKAnnotationView委托方法。

任何形式的帮助都将受到赞赏。

由于

3 个答案:

答案 0 :(得分:3)

[annotationView setCanShowCallout:NO]

这解决了我的问题,因为我不想在点击引脚时发出标注,但我想调用didSelectAnnotationView委托来调用。

由于

答案 1 :(得分:1)

您是否在注释中添加title属性?您确定[annotationView setCanShowCallout:YES]设置正确吗?

答案 2 :(得分:0)

创建mapview后是否设置了MKMapView的委托?