1

我在 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;
}
4

3 回答 3

1

您的代码无法假设调用该委托方法的时间,即使在 iOS 6 下,我猜想在某些情况下这会中断。

为什么不直接使用[annotation PinImage]而不是self.pinImageName?传递给的注释对象mapView:viewForAnnotation:与您传递给addAnnotation:上面的注释对象相同。

于 2013-10-24T11:42:33.840 回答
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;
    }
于 2014-01-29T05:48:21.173 回答
-1

确保为 MKMapView 对象设置了 MKMapViewDelegate。

你能检查一下吗?

于 2013-10-24T11:33:45.907 回答