更新 MKannotation 图像而不闪烁

发布于 2024-11-15 13:30:23 字数 1296 浏览 0 评论 0原文

我想每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

女中豪杰 2024-11-22 13:30:23

在注释不为零的情况下,不要添加和删除,而是尝试以下操作:

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

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;
千と千尋 2024-11-22 13:30:23

安娜答案的 Swift 版本:

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

Swift version of Anna answer:

annotation.customImage = ... //the new image
let av = self.mapView.viewForAnnotation(dnwl!)
av?.image = annotation.customImage
九局 2024-11-22 13:30:23

看来您正在使用自己的自定义视图进行注释,在这种情况下,您可以简单地向自定义视图添加“刷新”方法,并在更新底层注释后调用它(即:自定义视图 - 来自的派生类) 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];
}

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

@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
}

*) Where you handle the MKMapView and its annotations

for(CustomAnnotation *annotation in [m_MapView annotations])
{
    CustomAnnotationView *annotationView = [m_MapView viewForAnnotation:annotation];
    [annotationView refresh];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文