MKAnnotation 删除(处理器重)

发布于 2024-11-30 07:43:33 字数 2289 浏览 0 评论 0原文

此函数接受纬度/经度对的数组。它将它们全部转换为 MKAnnotation,然后对于地图上当前存在的每个注释,它检查它是否存在于新的注释集中。如果存在,则按原样保留注释,否则将其删除。

然后,对于每个新注释,它会检查它当前是否在地图上;如果是,则保留它,否则将其删除。

这显然非常密集,我想知道是否有更快的方法?

- (void)setAnnotationWithArray:(NSArray *)array {
    static BOOL processing = NO;
    if (processing) {
        return;
    }
    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        processing = YES;


        NSMutableArray *annotationsArray = [NSMutableArray arrayWithCapacity:[array count]];
        NSMutableArray *annotationsToRemove = [NSMutableArray array];

        for (NSDictionary *dict in array) {
            NSString *latStr = [dict objectForKey:@"Latitude"];
            NSString *lonStr = [dict objectForKey:@"Longitude"];
            NSString *title = [dict objectForKey:@"Location"];

            double lat = [latStr doubleValue];
            double lon = [lonStr doubleValue];


            CLLocationCoordinate2D location;
            location.latitude = lat;
            location.longitude = lon;

            MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:title andCoordinate:location];
            [annotationsArray addObject:newAnnotation];
            [newAnnotation release];
        }

        for (id<MKAnnotation> oldAnnotation in [mv annotations]) {
            CLLocationCoordinate2D oldCoordinate = [oldAnnotation coordinate];

            BOOL exists = NO;
            for (MapViewAnnotation *newAnnontation in annotationsArray) {
                CLLocationCoordinate2D newCoordinate = [newAnnontation coordinate];
                if ((newCoordinate.latitude == oldCoordinate.latitude)
                    && (newCoordinate.longitude == oldCoordinate.longitude)) {
                    exists = YES;
                    break;
                }
            }

            if (!exists) {
                [annotationsToRemove addObject:oldAnnotation];
            }
        }


        [annotationsArray removeObjectsInArray:[mv annotations]];
        dispatch_async( dispatch_get_main_queue(), ^{
            processing = NO;
            [mv removeAnnotations:annotationsToRemove];
            [mv addAnnotations:annotationsArray];
        });
    });



}

This function accepts an array of latitude/longitude pairs. It converts all of them into MKAnnotations, then for each of the annotations currently present on the map, it checks if it's present in the new set of annotations. If it is present, it leaves the annotation as is, otherwise removes it.

Then for each of the new annotations, it checks if it's currently on the map; if it is, it keeps it, otherwise removes it.

This is clearly very intensive, and I was wondering if there's a faster way of doing it?

- (void)setAnnotationWithArray:(NSArray *)array {
    static BOOL processing = NO;
    if (processing) {
        return;
    }
    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        processing = YES;


        NSMutableArray *annotationsArray = [NSMutableArray arrayWithCapacity:[array count]];
        NSMutableArray *annotationsToRemove = [NSMutableArray array];

        for (NSDictionary *dict in array) {
            NSString *latStr = [dict objectForKey:@"Latitude"];
            NSString *lonStr = [dict objectForKey:@"Longitude"];
            NSString *title = [dict objectForKey:@"Location"];

            double lat = [latStr doubleValue];
            double lon = [lonStr doubleValue];


            CLLocationCoordinate2D location;
            location.latitude = lat;
            location.longitude = lon;

            MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:title andCoordinate:location];
            [annotationsArray addObject:newAnnotation];
            [newAnnotation release];
        }

        for (id<MKAnnotation> oldAnnotation in [mv annotations]) {
            CLLocationCoordinate2D oldCoordinate = [oldAnnotation coordinate];

            BOOL exists = NO;
            for (MapViewAnnotation *newAnnontation in annotationsArray) {
                CLLocationCoordinate2D newCoordinate = [newAnnontation coordinate];
                if ((newCoordinate.latitude == oldCoordinate.latitude)
                    && (newCoordinate.longitude == oldCoordinate.longitude)) {
                    exists = YES;
                    break;
                }
            }

            if (!exists) {
                [annotationsToRemove addObject:oldAnnotation];
            }
        }


        [annotationsArray removeObjectsInArray:[mv annotations]];
        dispatch_async( dispatch_get_main_queue(), ^{
            processing = NO;
            [mv removeAnnotations:annotationsToRemove];
            [mv addAnnotations:annotationsArray];
        });
    });



}

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

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

发布评论

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

评论(1

傲鸠 2024-12-07 07:43:33

您可以使用removeObjectsInArray:(NSArray *)。

例如:

NSMutableArray *annotationsToRemove = [[NSMutableArray alloc] initWithArray:[mapView annotations]];
[annotationsToRemove removeObjectsInArray:annotationsArray];
NSMutableArray *annotationsToAdd = [[NSMutableArray alloc] initWithArray:annotationsArray];
[annotationsToAdd removeObjectsInArray:[mapView annotations]];

它假设您的注释实现了 hash 和 isEqual: 但应该更有效。

you could use removeObjectsInArray:(NSArray *).

for example:

NSMutableArray *annotationsToRemove = [[NSMutableArray alloc] initWithArray:[mapView annotations]];
[annotationsToRemove removeObjectsInArray:annotationsArray];
NSMutableArray *annotationsToAdd = [[NSMutableArray alloc] initWithArray:annotationsArray];
[annotationsToAdd removeObjectsInArray:[mapView annotations]];

It assumes that your annotations implement hash and isEqual: but should be more efficient.

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