iOS : App crashes when zooming out a map

时间秒杀一切 提交于 2019-12-11 07:14:52

问题


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 :

And when I zoom out :


回答1:


Try clustering. Basically you group together annotations.

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



来源:https://stackoverflow.com/questions/22515559/ios-app-crashes-when-zooming-out-a-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!