更新MKannotation图像而不闪烁

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

我已经尝试了以下内容:

//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]; } 

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

有没有人有任何想法?

在注释不是零的情况下,而不是添加和删除,试试这个:

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

安娜答案:

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

看起来你使用自己的自定义视图的注释,在这种情况下,你可以简单地添加一个“刷新”方法到你的自定义视图,并在你更新基础注释后调用它(即:自定义视图 – 派生类从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]; }