更新MKannotation图像而不闪烁
问题描述:
我想每5秒在mapview上更新一些注释的图像,但是我不想删除它们并将它们重新添加到地图中,因为这会导致它们闪烁或刷新,(即消失然后重新出现)。我希望它是无缝的。
I want to update the images of some of my annotations on a mapview every 5 seconds, however I dont' want to remove and re-add them to the map as this causes them to 'flash' or refresh, (ie disapear then reappear). I want it to be seamless.
我尝试了以下内容:
//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];
}
如果我在顶部添加了新图标我可以删除旧的图标,但这仍然导致闪烁。
as a thought that if I added the new icon on top I could then remove the old icon, but this still caused the flashing.
有没有人有任何想法?
Has anyone got any ideas?
答
如果注释不是nil,而不是添加和删除,请尝试:
In the case where the annotation is not nil, instead of adding and removing, try this:
annotation.customImage = ... //the new image
MKAnnotationView *av = [self.mapView viewForAnnotation:annotation];
av.image = annotation.customImage;