iOS : App crashes when zooming out a map

2019-08-01 05:18发布

I have this situation where my app crashes when I zoom out the map.

The problem arises because of the large number of annotations that I'm adding. Please have a look at my code below :

- (void) plotUsersInMap
{
for (id<MKAnnotation> annotation in self.mapView.annotations) {
    [self.mapView removeAnnotation:annotation];
}

NSUInteger count = //get total count
NSLog(@"count * %d", count);
for (int i = 0; i < count; i++)
{
    NSNumber *latitude = //get latitude from json
    NSNumber *longitude = //get longitude from json

    CLLocationCoordinate2D coordinate;
    coordinate.latitude = latitude.doubleValue;
    coordinate.longitude = longitude.doubleValue;

    @autoreleasepool {

        MyLocation *annotation = [[MyLocation alloc] initWithName:@"test" coordinate:coordinate QuestionId:nil];
       //annotations are added
        [self.mapView addAnnotation:annotation];
    }
}
}

Here I'm trying to add more than 400 pins which I think is the cause of crash [probably a memory leak!]. I would like to know if there is any way to add the pins one by one as I zoom out?

Map in initial stage, without any problem : Initial stage

And when I zoom out :

it crashes!!

1条回答
贼婆χ
2楼-- · 2019-08-01 05:37

Try clustering. Basically you group together annotations.

The code repo from the article I linked to: https://github.com/applidium/ADClusterMapView

查看更多
登录 后发表回答