0
votes

Why am I unable to remove my annotations from mapview?

My code:

  • (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self becomeFirstResponder];

    NSMutableArray *annotations = [[NSMutableArray alloc]init];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blackKey"]) { NSLog(@"Black is on");

    NSArray *ann = [dict objectForKey:@"Category1"];

    for(int i = 0; i < [ann count]; i++) {

    NSString *coordinates = [[ann objectAtIndex:i] objectForKey:@"Coordinates"];
    
    double realLatitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:1] doubleValue];
    double realLongitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:0] doubleValue];
    
    MyAnnotation *myAnnotation = [[MyAnnotation alloc] init];
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = realLatitude;
    theCoordinate.longitude = realLongitude;
    
    myAnnotation.coordinate=CLLocationCoordinate2DMake(realLatitude,realLongitude);
    
    myAnnotation.title = [[ann objectAtIndex:i] objectForKey:@"Name"];
    myAnnotation.subtitle = [[ann objectAtIndex:i] objectForKey:@"Address"];
    myAnnotation.icon = [[ann objectAtIndex:0] objectForKey:@"Icon"];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blackKey"])
    {
        NSLog(@"Black is on");
        [mapView addAnnotation:myAnnotation];
        [annotations addObject:myAnnotation];
    
    }   else 
        {
            NSLog(@"Black is off");
    
            [self.mapView removeAnnotation:myAnnotation];
    
    
        } 
    
    }
    

    }

    else { //Do nothing }

     }
    

[self.mapView removeAnnotation:myAnnotation]; does not work for me

1

1 Answers

0
votes

There is nothing to remove. You create the annotation, and then based off the check for blackKey, you EITHER add or remove it. But when you remove it, you never added it prior to that.