处理 MKMapView 上的 MKAnnotations 时速度变慢

发布于 2024-11-29 10:12:39 字数 970 浏览 0 评论 0原文

我正在逐步浏览地图上的注释,以便找到具有特定标签的注释。然后我删除这个注释,并添加一个新的注释(不同的颜色)。

问题是这需要非常非常长的时间。不知道为什么。它会处理 300 个注释,最多需要 20 秒!我怎样才能加快速度?例如,有没有办法只获取当前可见的注释?或者,如果我有注释,我可以告诉它在 mapView.annotations 数组中的索引是什么,这样我就不必单步执行吗?

for (int i =0; i < [self.mapView.annotations count]; i++) 
{
        if ([[self.mapView.annotations objectAtIndex:i] isKindOfClass:[BasicMapAnnotation class]] ) 
        { 
            BasicMapAnnotation *bmaTemp =(BasicMapAnnotation*)[self.mapView.annotations objectAtIndex:i];
            if (bmaTemp.mapTag == pinTag) {
                [self.mapView removeAnnotation:[self.mapView.annotations objectAtIndex:i]];
                self.customAnnotation = [[[BasicMapAnnotation alloc] initWithLatitude:[shop.latitude doubleValue] andLongitude:[shop.longitude doubleValue]] autorelease];
                self.customAnnotation.mapTag = pinTag;
                [self.mapView addAnnotation:self.customAnnotation];
            }

        }
}

I'm stepping thru the annotations on my map, in order to find one which has a certain tag. I then remove this annotation, and add a new one (a diff color)

The problem is that this takes a very, very long time. Not sure why. It's going thru 300 annotations, and it takes up to 20 seconds! How can I speed this up? It there a way to, for instance, only get the annotations which are currently visible? Or, if I have the annotation, can I tell what is its index in mapView.annotations array is, so I don't have to step thru?

for (int i =0; i < [self.mapView.annotations count]; i++) 
{
        if ([[self.mapView.annotations objectAtIndex:i] isKindOfClass:[BasicMapAnnotation class]] ) 
        { 
            BasicMapAnnotation *bmaTemp =(BasicMapAnnotation*)[self.mapView.annotations objectAtIndex:i];
            if (bmaTemp.mapTag == pinTag) {
                [self.mapView removeAnnotation:[self.mapView.annotations objectAtIndex:i]];
                self.customAnnotation = [[[BasicMapAnnotation alloc] initWithLatitude:[shop.latitude doubleValue] andLongitude:[shop.longitude doubleValue]] autorelease];
                self.customAnnotation.mapTag = pinTag;
                [self.mapView addAnnotation:self.customAnnotation];
            }

        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

要走就滚别墨迹 2024-12-06 10:12:39

如果您确定要修改的注释实际上是可见的,您可以使用类似的内容

for (id annotation in [self.mapView annotationsInMapRect:self.mapView.visibleMapRect]){
    if([annotation isKindOfClass:[BasicMapAnnotation class]]){
        BasicMapAnnotation *annotationToModify = annotation;
        if (bmaTemp.mapTag == pinTag) {
            [self.mapView removeAnnotation:annotationToModify];
            self.customAnnotation = [[[BasicMapAnnotation alloc] initWithLatitude:[shop.latitude doubleValue] andLongitude:[shop.longitude doubleValue]] autorelease];
            self.customAnnotation.mapTag = pinTag;
            [self.mapView addAnnotation:self.customAnnotation];
        }
    }
}

如果您想知道单个给定注释的索引:

int annotationIndex = [self.mapView.annotations indexOfObject:givenAnnotation];

未经测试,我在这里写了这个所以请让我知道它是否有效或不。

If you are sure that the Annotation you want to modify is actually visible, you could use something like

for (id annotation in [self.mapView annotationsInMapRect:self.mapView.visibleMapRect]){
    if([annotation isKindOfClass:[BasicMapAnnotation class]]){
        BasicMapAnnotation *annotationToModify = annotation;
        if (bmaTemp.mapTag == pinTag) {
            [self.mapView removeAnnotation:annotationToModify];
            self.customAnnotation = [[[BasicMapAnnotation alloc] initWithLatitude:[shop.latitude doubleValue] andLongitude:[shop.longitude doubleValue]] autorelease];
            self.customAnnotation.mapTag = pinTag;
            [self.mapView addAnnotation:self.customAnnotation];
        }
    }
}

If you want to know the index of a single given annotation:

int annotationIndex = [self.mapView.annotations indexOfObject:givenAnnotation];

Untested, I wrote this here on SO so please let me know, whether it works or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文