开发者

MKAnnotation removing (processor heavy)

开发者 https://www.devze.com 2023-03-29 12:13 出处:网络
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

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)setAnnotatio开发者_C百科nWithArray:(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];
        });
    });



}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消