1

i have used this lines of code to add more than 2000 custom annotation on MkMapView. Then first received memory warning and app crashed.

 -(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id   <MKAnnotation>)annotation
{

    if([annotation isKindOfClass:[MKUserLocation class]])
    {
        return nil;
    }
    //MKAnnotationView *annotationView = nil;
   else if ([annotation isKindOfClass:[MyAnnotation class]])
    {
        MKAnnotationView *annotationView = nil;
        static NSString *identifier = @"identifier";
        annotationView = (MKAnnotationView *)[mapViewForMapScreen dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView) {
            annotationView.annotation = annotation;
        } else {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            annotationView.canShowCallout = YES;
        }

        MyAnnotation *anno=(MyAnnotation*)annotation;
        tagForAnnotation=(int)anno.tag;

        if (arrayForLatLong.count!=0)
        {
            if ([[[arrayForLatLong valueForKey:@"sourceType"]  objectAtIndex:tagForAnnotation] isEqualToString:@"INSTAGRAM"])
            {
                annotationView.image=[UIImage imageNamed:@"instagram.png"];
                annotationView.frame=CGRectMake(0, 0, 16, 22);
            }
            else if([[[arrayForLatLong valueForKey:@"sourceType"]  objectAtIndex:tagForAnnotation] isEqualToString:@"TWITTER"])
            {
                annotationView.image=[UIImage imageNamed:@"twt_pin.png"];
                annotationView.frame=CGRectMake(0, 0, 16, 22);
            }

            else if ([[[arrayForLatLong valueForKey:@"sourceType"]  objectAtIndex:tagForAnnotation] isEqualToString:@"YOUTUBE"])
            {
                annotationView.image=[UIImage imageNamed:@"youtube.png"];
                annotationView.frame=CGRectMake(0, 0, 16, 22);
            }
            else if ([[[arrayForLatLong valueForKey:@"sourceType"]  objectAtIndex:tagForAnnotation] isEqualToString:@"MEETUP"])
            {
                annotationView.image=[UIImage imageNamed:@"meetup_iphone"];
                annotationView.frame=CGRectMake(0, 0, 16, 22);
            }
            else if ([[[arrayForLatLong valueForKey:@"sourceType"]  objectAtIndex:tagForAnnotation] isEqualToString:@"FLICKR"])
            {
                annotationView.image=[UIImage imageNamed:@"flikr.png"];
                annotationView.frame=CGRectMake(0, 0, 16, 22);
            }
            else if ([[[arrayForLatLong valueForKey:@"sourceType"]  objectAtIndex:tagForAnnotation] isEqualToString:@"VK"])
            {
                annotationView.image=[UIImage imageNamed:@"vk1"];
                annotationView.frame=CGRectMake(0, 0, 16, 22);
            }
        }
        infoButton=[[AsyncImageView alloc]init];
        if ([[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"profilePic"] valueForKey:@"small"] isEqualToString:@""]){
            if ([[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"profilePic"] valueForKey:@"medium"] isEqualToString:@""]){
                if ([[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"profilePic"] valueForKey:@"large"] isEqualToString:@""]) {
                    [infoButton setImage:[UIImage imageNamed:@"profile"] forState:UIControlStateNormal];
                }else{
                    [infoButton setImageURL:[NSURL URLWithString:[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"profilePic"] valueForKey:@"large"]]];
                }
            }else{
                [infoButton setImageURL:[NSURL URLWithString:[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"profilePic"] valueForKey:@"medium"]]];
            }
        }else{
            [infoButton setImageURL:[NSURL URLWithString:[[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"]valueForKey:@"profilePic"] valueForKey:@"small"]]];
        }
        [infoButton setFrame:CGRectMake(20,10,30,30)];
        infoButton.layer.cornerRadius=infoButton.frame.size.height/2.0;
        infoButton.layer.masksToBounds = YES;
        [infoButton addTarget:self action:@selector(actionForProfileImage:) forControlEvents:UIControlEventTouchUpInside];
        infoButton.layer.borderWidth=1.0;
        infoButton.layer.borderColor=[UIColor grayColor].CGColor;
        infoButton.tag=tagForAnnotation;


        //Followed Button:----
        AsyncImageView *followedBtn=[[AsyncImageView alloc]init];
        [followedBtn setFrame:CGRectMake(2,8,17,17)];
        followedBtn.tag=tagForAnnotation;
        if ([[[[arrayForLatLong objectAtIndex:tagForAnnotation] valueForKey:@"user"] valueForKey:@"isFollowed"] boolValue]==0 ){
            [followedBtn setBackgroundImage:[UIImage imageNamed:@"star_grey.png"] forState:UIControlStateNormal];
        }else{
            [followedBtn setBackgroundImage:[UIImage imageNamed:@"star_blue.png"] forState:UIControlStateNormal];
        }
        [followedBtn addTarget:self action:@selector(actionForStarImage:) forControlEvents:UIControlEventTouchUpInside];
        UIView*viewForCallOut=[[UIView alloc] initWithFrame:CGRectMake(0, 0,50, 50)];
        [viewForCallOut addSubview:infoButton];
        //[viewForCallOut addSubview:followedBtn];
        annotationView.leftCalloutAccessoryView=viewForCallOut;
        annotationView.canShowCallout = YES;
        return annotationView;
    }


    return nil;
}

Please check my code if i have done any mistake then please let me know. Thanks in advance

KSR
  • 1,699
  • 15
  • 22
  • 1
    You got a memory warning, and it crashed because of it: The ressources of an iDevice is not infinite. In order to "fix" the issue, check if there is no memory leak (with Instruments/Memory Leak), and you may want to avoid that 2000 custom annotations (are they all useful? all visibles?) – Larme Sep 13 '16 at 11:48
  • @Larme Yes they all are useful because i also create on event on custom call out view for all custom annotation with different different information. – Subhash Mehta Sep 13 '16 at 12:21
  • you can load only those notations which are visible as per the map area. Add notations as map zoom. – NewStackUser Sep 13 '16 at 12:32
  • @NewStackUser i already zoom map for 5 miles then i need to display all annotations of 5 miles at a time.and i have lots of data to display.what should i do in this condition? – Subhash Mehta Sep 13 '16 at 12:42

1 Answers1

0

When you have a large number of data points that result in a large number of annotations, a good approach is to cluster the data points and add an annotation for each cluster. As the map is zoomed into a cluster, you replace the annotation representing the cluster with annotations for the data points in the cluster. As you zoom out of a cluster, you replace the annotations representing the data points with an annotation that represents the cluster. This approach reduces the total number of annotations and makes the data easier to comprehend. This SO question and this post will give you more information about clustering.

Community
  • 1
  • 1
Jeffrey Morgan
  • 556
  • 1
  • 6
  • 12