在ios7中添加注释时,Mapview委托不调用

时间:2013-10-24 11:26:31

标签: iphone objective-c ios7 mkmapview

我正在mapview上删除多个引脚,我需要为所有引脚显示不同的图像,我正在执行下面的代码。

- (void)dropMultiplePinAnnotation:(NSArray *)arrThumbnail
{
    [self removeAnnotations:[self annotations]];

    for(Annotation *Annot in arrThumbnail) {

        self.pinImageName = Annot.PinImage;
        Annot.coordinate = Detail.coordinate;
        [self addAnnotation:Annot];
        //Till ios6, after this line viewForAnnotation gets called so that I can assign new pin image everytime but in ios7 viewforAnnotation called after this loop ends so it set pin image which was latest saved in self.pinImageName
    }    
}

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *AnnotViewID = @"annotViewID";
    MKAnnotationView *annotationView = (MKAnnotationView *)[self dequeueReusableAnnotationViewWithIdentifier:AnnotViewID];

    if (annotationView == nil) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotViewID];
    }
    annotationView.canShowCallout = NO;

    annotationView.image = [UIImage imageNamed:self.pinImageName];
    annotationView.annotation = annotation;

    return annotationView;
}

3 个答案:

答案 0 :(得分:1)

您的代码无法假设调用该委托方法的时间,即使在iOS 6下我也会猜测有些情况会破坏。

为什么不使用[annotation PinImage]代替self.pinImageName?传递给mapView:viewForAnnotation:的注释对象与您传递给addAnnotation:的注释对象相同。

答案 1 :(得分:0)

我正在mapview上删除多个引脚,我需要为所有引脚显示不同的图像,我正在执行下面的代码。

- (void)dropMultiplePinAnnotation:(NSArray *)arrThumbnail

    {
        [self removeAnnotations:[self annotations]];

        for(Annotation *Annot in arrThumbnail) {

            self.pinImageName = Annot.PinImage;
            Annot.coordinate = Detail.coordinate;
            [self addAnnotation:Annot];
       [_mapView setRegion:MKCoordinateRegionMake(Annot.coordinate,      MKCoordinateSpanMake(0.004,0.004))];
            //So you can fire every time viewForAnnotation delegate after every annotation adding to MKMapView
        }    
    }

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
    {

    static NSString *AnnotViewID = @"annotViewID";
        MKAnnotationView *annotationView = (MKAnnotationView *)[self dequeueReusableAnnotationViewWithIdentifier:AnnotViewID];

        if (annotationView == nil) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotViewID];
        }
        annotationView.canShowCallout = NO;

        annotationView.image = [UIImage imageNamed:self.pinImageName];
        annotationView.annotation = annotation;

        return annotationView;
    }

答案 2 :(得分:-1)

确保为MKMapView对象设置了MKMapViewDelegate。

你能检查一下吗?