2010-11-26 4 views
0

J'essaie de supprimer toutes les annotations d'une carte. La classe qui contient le MKMapView implémente le MKMapViewDelegate. J'appelle la méthode remove all annotations lorsqu'un bouton (affiché dans une vue modale) est cliqué. J'utilise le code suivant pour supprimer toutes les annotations.Problèmes de suppression des annotations d'un MKMapView

- (void)removeAllAnnotations { 

NSMutableArray *annotationsToRemove = [NSMutableArray arrayWithCapacity:[self.mapView.annotations count]]; 
for (int i = 0; i < [self.mapView.annotations count]; i++) { 
    if ([[self.mapView.annotations objectAtIndex:i] isKindOfClass:[AddressAnnotation class]]) { 
    [annotationsToRemove addObject:[self.mapView.annotations objectAtIndex:i]]; 
    } 
} 

[self.mapView removeAnnotations:annotationsToRemove]; 
} 

Le code fonctionne bien, mais après avoir appelé la méthode, et lorsque je tente d'ajouter de nouvelles annotations à la carte vide, la classe ne remet pas à la méthode viewForAnnotations et les annotations ne dégoutte pas et ne montre pas un bouton de divulgation dans la vue d'annotation. Pourquoi cela arrive-t-il?

Merci d'avoir lu.

Modifié:

Les annotations sont présentés mais en appelant la vue pour la méthode d'annotation (avec en laissant tomber vers le bas et en incluant un bouton d'information dans la vue d'annotation). Voici la méthode que j'utilise pour ajouter les annotations et la méthode viewForAnnotation.

- (void)loadAnnotations:(NSString *)type { 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"PlacesInformation" ofType:@"plist"]; 
    NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path]; 

    for (int i = 0; i < tmpArray.count; i++) { 

     // Breaks the string down even further to latitude and longitude fields. 
     NSString *coordinateString = [[tmpArray objectAtIndex:i] objectForKey:@"coordinates"]; 

     NSString *option = [[tmpArray objectAtIndex:i] objectForKey:@"type"];    
     if ([option isEqualToString:type]) { 
      CLLocationCoordinate2D currentCoordinate = [self stringToCoordinate:coordinateString]; 
      AddressAnnotation *annotation = [[[AddressAnnotation alloc] initWithCoordinate:currentCoordinate] autorelease]; 
      [self.mapView addAnnotation:annotation]; 
     } 
    } 
} 

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

    // If it's the user location, just return nil. 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 
    else { // Handles the other annotations. 
     // Try to dequeue an existing pin view first. 
     static NSString *AnnotationIdentifier = @"AnnotationIdentifier"; 
     MKPinAnnotationView *pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier]; 

     if (!pinView) { 
      // If an existing pin view was not available, creates one. 
      MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease]; 
      customPinView.animatesDrop = YES; 
      customPinView.canShowCallout = YES; 

      // Adds a detail disclosure button. 
      UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
      [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside]; 
      customPinView.rightCalloutAccessoryView = rightButton; 

      return customPinView; 
     } else 
      pinView.annotation = annotation; 
    } 

    return nil; 
} 
+1

Vous nous montrer le code qui fonctionne et ne nous montre pas le code qui ne fonctionne pas? –

+0

Montrez comment vous ajoutez les nouvelles annotations. Les nouvelles annotations apparaissent-elles sur la carte? – Anna

Répondre

0

Dans viewForAnnotation, dans le cas où vous réutilisez une vue, vous renvoyez actuellement zéro.

Cette partie else:

} else 
    pinView.annotation = annotation; 

devrait probablement être:

} else { 
    pinView.annotation = annotation; 
    return pinView; 
} 
+0

Je vous assure que je l'avais révisé un million de fois. C'était le problème. Merci beaucoup aBitObvious. –

+0

Je travaille sur le projet mapview. J'ai une question liée à la suppression des annotations de mapView. J'ai implémenté le code suivant mais il supprime les annotations aléatoirement, pas le premier! [mapView removeAnnotation: [self.mapView.annotations objectAtIndex: 0]]; –

Questions connexes