为什么我无法从mapview中删除我的注释?

时间:2012-06-01 21:17:07

标签: ios xcode annotations

为什么我无法从mapview中删除我的注释?

我的代码:

  
      
  • (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在");

         

    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];
    
    
        } 
    
    }
    
         

    }

         

    否则   {       //没做什么   }

     }
    
  •   

[self.mapView removeAnnotation:myAnnotation];对我不起作用

1 个答案:

答案 0 :(得分:0)

没有什么可以删除的。您创建注释,然后基于blackKey检查,您可以添加或删除它。但是当你删除它时,你从未在它之前添加它。