更新 MKannotation 图像而不闪烁
我想每 5 秒更新一次地图视图上的一些注释的图像,但是我不想将它们删除并重新添加到地图中,因为这会导致它们“闪烁”或刷新(即消失然后重新出现) 。我希望它是无缝的。
我尝试了以下操作:
//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];
}
认为如果我在顶部添加新图标,我就可以删除旧图标,但这仍然导致闪烁。
有人有什么想法吗?
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.
I've tried the following:
//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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在注释不为零的情况下,不要添加和删除,而是尝试以下操作:
In the case where the annotation is not nil, instead of adding and removing, try this:
安娜答案的 Swift 版本:
Swift version of Anna answer:
看来您正在使用自己的自定义视图进行注释,在这种情况下,您可以简单地向自定义视图添加“刷新”方法,并在更新底层注释后调用它(即:自定义视图 - 来自的派生类) MKAnnotationView- 始终附加到符合 MKAnnotation 协议的潜在自定义“注释”类)
*) CustomAnnotationView.h
*) CustomAnnotationView.m
*) 处理 MKMapView 及其注释的位置
It seems you are using your own custom views for the annotations, in that case you can simply add a "refresh" method to your custom view and call it after you have updated the underlying annotation (ie: a custom view -a derived class from MKAnnotationView- is always attached to a potentially custom "annotation" class that conforms to the MKAnnotation protocol)
*) CustomAnnotationView.h
*) CustomAnnotationView.m
*) Where you handle the MKMapView and its annotations