更新MKannotation图像而不闪烁

时间:2011-06-16 16:40:45

标签: objective-c ios mapkit

我想每5秒在mapview上更新一些注释的图像,但是我不想删除它们并将它们重新添加到地图中,因为这会导致它们“闪烁”或刷新,(即消失然后重新出现)。我希望它是无缝的。

我尝试了以下内容:

//get the current icon
        UserAnnotation *annotation = [self GetUserIconWithDeviceId:deviceId];

        //make a new annotation for it 
        UserAnnotation *newAnnotation = [[UserAnnotation alloc]
                                         initWithCoordinate: userCoordinates
                                         addressDictionary:nil];
        newAnnotation.title = name;
        newAnnotation.userDeviceId = deviceId;
        NSInteger ageIndicator = [[userLocation objectForKey: @"ageIndicator"] integerValue];
        newAnnotation.customImage = [UserIconHelpers imageWithAgeBorder:ageIndicator FromImage: userImage];



        //if its not there, add it 
        if (annotation != nil){
            //move it 

            //update location
            annotation.coordinate = userCoordinates;

            //add new one 
            [self.mapView addAnnotation: newAnnotation];

            //delete old one 
            [self.mapView removeAnnotation: annotation];

        } else {
        //just addd the new one 
            [self.mapView addAnnotation: newAnnotation]; 
        }

作为一种想法,如果我在顶部添加新图标,我可以删除旧图标,但这仍然导致闪烁。

有没有人有任何想法?

3 个答案:

答案 0 :(得分:8)

如果注释不是nil,而不是添加和删除,请尝试:

annotation.customImage = ... //the new image
MKAnnotationView *av = [self.mapView viewForAnnotation:annotation];
av.image = annotation.customImage;

答案 1 :(得分:1)

安娜版的安娜回答:

annotation.customImage = ... //the new image
let av = self.mapView.viewForAnnotation(dnwl!)
av?.image = annotation.customImage

答案 2 :(得分:-1)

您似乎使用自己的自定义视图进行注释,在这种情况下,您只需向自定义视图添加“刷新”方法,并在更新基础注释后调用它(即:自定义视图-a来自MKAnnotationView的派生类始终附加到符合MKAnnotation协议的潜在自定义“注释”类中。

*)CustomAnnotationView.h

@interface CustomAnnotationView : MKAnnotationView
{
    ...
}
...

//tell the view to re-read the annotation data it is attached to
- (void)refresh;

*)CustomAnnotationView.m

//override super class method
- (void)setAnnotation:(id <MKAnnotation>)annotation
{
    [super setAnnotation:annotation];
    ...
    [self refresh];
}
- (void)refresh
{
    ...
    [self setNeedsDisplay]; //if necessary
}

*)你在哪里处理MKMapView及其注释

for(CustomAnnotation *annotation in [m_MapView annotations])
{
    CustomAnnotationView *annotationView = [m_MapView viewForAnnotation:annotation];
    [annotationView refresh];
}