Search code examples
memory-managementmkmapviewmkannotationmapkit

MKMapView with infinite random annotations


I am trying to populate a MKMapView with an infinite amount of annotations that are viewable as the user scrolls through the map. Obviously the user has to be zoomed far enough for the views to appear because if not it would be too much for the app to handle, only about 20 are shown at a time.

I have an array of about 100 objects that I need to randomly repeat throughout the map at random locations. These are created during runtime using MKMapView's visibleMapRect property to only create the necessary ones. I also implemented a cache dictionary to prevent recreating previously created objects. Here is my simplified current code:

@property (nonatomic, strong) NSArray *mapObjects; //Contains 100 objects to be repeated
@property (nonatomic, strong) NSMutableDictionary *cache; //Cache already created mapObjects

//Constants used
int const MapDensity  = 5000.0; //Density of annotations
int const MapZoomMax  = 30000.0; //Max zoom level to show annotations

- (void)loadLocations {
    MKMapRect rect = [mapView visibleMapRect];

    if (rect.size.width > MapZoomMax) {
    [self performSelectorOnMainThread:@selector(removeAllAnnotations) withObject:nil waitUntilDone:NO];
        return;
    }

    rect.origin.x = MapDensity*floor(rect.origin.x/MapDensity);
    rect.origin.y = MapDensity*floor(rect.origin.y/MapDensity);

    MKMapPoint pointLocation = rect.origin;
    NSMutableArray *locationsArray = [NSMutableArray array];

    while (pointLocation.y < rect.origin.y+rect.size.height) {
        while (pointLocation.x < rect.origin.x+rect.size.width) {
            int cacheKey = pointLocation.x*pointLocation.y;
            if (![self.cache objectForKey:[NSNumber numberWithInt:cacheKey]]) {

                //Adjust for randomness
                MKMapPoint pointLocationAdjusted = pointLocation;
                pointLocationAdjusted.x += arc4random()%MapDensity;
                pointLocationAdjusted.y += arc4random()%MapDensity;

                //Create annotation
                GLAnnotation *annotation = [[GLAnnotation alloc] init];
                [annotation setCoordinate:MKCoordinateForMapPoint(pointLocationAdjusted)];
                [annotation setMapObject:[self.mapObjects objectAtIndex:(arc4random()%[mapObjects count])]];
                [locationsArray addObject:annotation];

                [self.cache setObject:annotation forKey:[NSNumber numberWithInt:cacheKey]];
            } else {
                [locationsArray addObject:[self.cache objectForKey:[NSNumber numberWithInt:cacheKey]]];
            }
            pointLocation.x += MapDensity; //Go to next X
        }
        pointLocation.x = rect.origin.x; //Restart X
        pointLocation.y += MapDensity; //Go to next Y
    }

    [self performSelectorOnMainThread:@selector(addAnnotations:) withObject:locationsArray waitUntilDone:NO];
}

- (void)addAnnotations:(NSArray *)annotations {
    NSMutableArray *newAnnotations = [NSMutableArray array];
    for (id annotation in annotations) {
        if (![mapView.annotations containsObject:annotation]) {
            [mapView addObject:annotation];
        }
    }
    [mapView addAnnotations:newAnnotations];
}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    [self performSelectorInBackground:@selector(loadLocations) withObject:nil];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if([annotation isKindOfClass:[GLAnnotation class]]){
        static NSString *annotationIdentifier = @"AnnotationIdentifier";

        MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
        if(!annotationView){
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
            annotationView.canShowCallout = YES;
        }

        annotationView.image = [(GLAnnotation *)annotation picture];
        return annotationView;
    }
    return nil;
}

The code seems to work most of the time but it sometimes makes the map scrolling lag (I tried to fix that running it in a background thread) and usually makes the app crash, seems to be caused by a memory problem (which I tried to fix using the cache). Does not output any error message except: EXC_BAD_ACCESS. I would be very grateful if someone could tell me how to manage that amount of annotations correctly.


Solution

  • I think you might be generating 25,000,000 more items than you need to. But please check my maths. You're creating one annotation for each coordinate pair starting from rect.origin and incrementing by MapDensity. So one object for each 5000x5000 block. The first one being cached (0,0). But if the map view changed by just one mappoint, you'd have a whole new object for (0,1) and the following 5000x5000 block. Perhaps you should shift your starting point to the next lowest 5000 mappoint and increment from there. That way you will get to reuse the same object all the way until your starting point leaves the 5000x5000 block.

    Also, they hash for your cache leads to collisions. (1,12), (2,6), (3,4), (4,3), (6,2), (12,1), (-1,-12), (-2,-6) etc etc, all hash to the same value. Since a key can be a string try [NSString stringWithFormat:@"%d_%d",x,y] instead.