Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum number of annotations (MKAnnotation) that can be drawn on MKMapView?

I want to add a number of annotations(arround 500) on a mapview but the maximum it seems to display is 100. If I go beyond 100, viewForAnnotation delegate method is not called. However it works perfectly for annotations below 100.

here is the code: (works only when count of annotationArray array is less than 101)

_allPoints = [[NSMutableArray alloc] init];

NSString* responseFile = [[NSBundle mainBundle] pathForResource:@"Datafile" ofType:@"txt"];
NSData *sampleData = [NSData dataWithContentsOfFile:responseFile];  
if (sampleData) {  
    NSError* error;
    NSDictionary* json = [NSJSONSerialization 
                          JSONObjectWithData:sampleData

                          options:kNilOptions 
                          error:&error];

    NSArray* response = [json objectForKey:@"response"];
    for (NSDictionary *lineDict in response) {
        if ([[lineDict objectForKey:@"data"] isKindOfClass:[NSArray class]]) {
            SinglePoint *singlePoint = [[SinglePoint alloc] initWithDictionary:lineDict];
            [_allPoints addObject:singlePoint];
        }
        else {
            NSLog(@"Error");
        }
    }
}  


_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[_mapView setDelegate:self];
[self.view addSubview:_mapView];

NSMutableArray *annotationArray = [[NSMutableArray alloc] init];
for (int i=0; i<[_allPoints count]; i++) {
    SinglePoint *singlePoint = [_allPoints objectAtIndex:i];
    NVPolylineAnnotation *annotation = [[NVPolylineAnnotation alloc] initWithPoint:singlePoint mapView:_mapView];
    [annotationArray addObject:annotation];
}
[_mapView addAnnotations:(NSArray *)annotationArray];

CLLocationCoordinate2D centerCoord = { 28.632747, 77.219982 };
[_mapView setCenterCoordinate:centerCoord zoomLevel:12 animated:NO];

The delegate method is:

EDIT: As per comments, started reusing the view but with no luck :(

if ([annotation isKindOfClass:[NVPolylineAnnotation class]]) {
    static NSString *viewIdentifier = @"annotationView";
    NVPolylineAnnotationView *annotationView = (NVPolylineAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:viewIdentifier];
    if (annotationView == nil) {
        annotationView = [[NVPolylineAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:viewIdentifier mapView:_mapView];
    }
    return annotationView;
}
return nil;

I could not find any restriction in the documentation or anywhere else. Could it be memory issue?

like image 468
Shri Avatar asked Nov 05 '22 01:11

Shri


2 Answers

The MKMapView has not Limit for annotationViews. But it gets quite laggy and unusable above a certain number of views (+1000).

I believe, that the reason for this is that you handle the annotationView management totally wrong. You shouldn't create a unique view for every single annotation, even if you are using ARC. Your rather should reuse every unused View like the cell of a UITableView.

There are a couple of good tutorials for this like Introduction to MapKit on iOS - Ray Wenderlich.

If this won't resolve your problem, you should try to debug your annotation classes. (NVPolylineAnnotation and NVPolylineAnnotationView). Maybe there's something wrong. Did you also checked your points array for equal coordinates?

like image 88
yinkou Avatar answered Nov 15 '22 06:11

yinkou


Finally was able to track down the problem. Solved it by setting the center of mapView first and then adding annotations later. I still dont know why 100 annotations were shown earlier (and why the no 100 only). Thank you all for your suggestions and time on this.

This is the code I changed:-

_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[_mapView setDelegate:self];
[self.view addSubview:_mapView];

CLLocationCoordinate2D centerCoord = { 28.632747, 77.219982 };
[_mapView setCenterCoordinate:centerCoord zoomLevel:12 animated:NO];

NSMutableArray *annotationArray = [[NSMutableArray alloc] init];
for (int i=0; i<[_allPoints count]; i++) {
    SinglePoint *singlePoint = [_allPoints objectAtIndex:i];
    NVPolylineAnnotation *annotation = [[NVPolylineAnnotation alloc] initWithPoint:singlePoint mapView:_mapView];
    [annotationArray addObject:annotation];
}
[_mapView addAnnotations:(NSArray *)annotationArray];
like image 23
Shri Avatar answered Nov 15 '22 04:11

Shri