2010-01-25 10 views
25

Ich möchte alle Anmerkungen aus meiner Mapview ohne den blauen Punkt meiner Position entfernen. Wenn ich anrufe:Wie entfernen Sie alle Anmerkungen aus MKMapView, ohne den blauen Punkt zu entfernen?

[mapView removeAnnotations:mapView.annotations]; 

Alle Anmerkungen werden entfernt.

In welcher Weise kann ich (wie eine for-Schleife für alle Annotationen) prüfen, ob die Annotation nicht die blaue Punktannotation ist?

EDIT (ich mit diesem gelöst haben):

for (int i =0; i < [mapView.annotations count]; i++) { 
    if ([[mapView.annotations objectAtIndex:i] isKindOfClass:[MyAnnotationClass class]]) {      
     [mapView removeAnnotation:[mapView.annotations objectAtIndex:i]]; 
     } 
    } 
+0

Hey Mat, habe ich versucht, den Code verwenden, und es funktioniert, wenn aus irgendeinem Grunde statt einen Stift zu einem Zeitpunkt, zu entfernen es von 3 oder 2 zu einer Zeit entledigt. ...was ist damit? – skinny123

+0

versuchen, die Interaktion umzukehren. Offensichtlich bedeutet das Entfernen von eins, dass sich Ihre Indizes ändern. Entfernen Sie von der Rückseite. – chrism

+0

mögliches Duplikat von [Wie entferne ich alle Anmerkungen aus MKMapView mit Ausnahme der Annotation des Benutzerstandorts?] (Http://stackoverflow.com/questions/10865088/how-do-i-remove-all-annotations-from-mkmapview-except -the-user-location-annotati) – nielsbot

Antwort

58

am MKMapView documentation Blick scheint es, wie Sie die Anmerkungen Eigenschaft haben, zu spielen. Es sollte ziemlich einfach sein, durch diese zu durchlaufen und sehen, was Anmerkungen, die Sie haben:

for (id annotation in myMap.annotations) { 
    NSLog(@"%@", annotation); 
} 

Sie haben auch die userLocation Eigenschaft, die Sie die Anmerkung gibt, die den Ort des Benutzers. Wenn Sie die Anmerkungen zu gehen und sie alle erinnern, die nicht der Benutzer Lage sind, können Sie dann entfernen sie die removeAnnotations: Methode:

NSInteger toRemoveCount = myMap.annotations.count; 
NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:toRemoveCount]; 
for (id annotation in myMap.annotations) 
    if (annotation != myMap.userLocation) 
     [toRemove addObject:annotation]; 
[myMap removeAnnotations:toRemove]; 

hoffe, das hilft,

Sam

+0

hallo danke für deinen trick Sam !, habe ich jetzt auch auf diese weise gelöst: für (int i = 0; i <[mapView.Anmerkungen zählen]; i ++) { \t \t if ([[mapView.annotations objectAtIndex: i] isKindOfClass: [MyAnnotationClass Klasse]]) { [mapView RemoveAnnotation: [mapView.annotations objectAtIndex: i]]; \t \t} \t} .thanks wieder .. :) – Mat

+0

Das, es zu tun zu ein ziemlich guter Weg ist - ich alle Anmerkungen bin zu entfernen, aber Sie entfernen nur die Anmerkungen, die Sie, die hinzugefügt haben, ist wahrscheinlich eine sicherere Sache zu tun. Schön. S – deanWombourne

+1

Mat, nur ein Gedanke: Wird nicht eine Anmerkung übersprungen, die anstelle einer entfernten verschoben wird? Scheint, dass die Annotation nach dem Index erhalten würde, den der Entfernte hatte, aber der I-Zähler erhöht gnadenlos. –

31

Wenn Sie Wie schnell und einfach, gibt es eine Möglichkeit, ein Array der MKUserLocation-Annotation zu filtern. Sie können dies in MKMapView removeAnnotations: -Funktion übergeben.

[_mapView.annotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"!(self isKindOfClass: %@)", [MKUserLocation class]]]; 

Ich nehme an, das oben gepostet so ziemlich die gleiche wie die manuellen Filter ist, außer ein Prädikat mit der schmutzigen Arbeit zu tun.

6
for (id annotation in map.annotations) { 
    NSLog(@"annotation %@", annotation); 

    if (![annotation isKindOfClass:[MKUserLocation class]]){ 

     [map removeAnnotation:annotation]; 
    } 
    } 

i modifiziert wie dieses

13

Ist es nicht einfacher, nur Folgendes tun:

//copy your annotations to an array 
    NSMutableArray *annotationsToRemove = [[NSMutableArray alloc] initWithArray: mapView.annotations]; 
//Remove the object userlocation 
    [annotationsToRemove removeObject: mapView.userLocation]; 
//Remove all annotations in the array from the mapView 
    [mapView removeAnnotations: annotationsToRemove]; 
    [annotationsToRemove release]; 
+5

Dies ist die beste Antwort! –

+1

funktioniert für mich, danke. –

1

es einfacher zu machen, nur die folgenden:

NSMutableArray *annotationsToRemove = [NSMutableArray arrayWithCapacity:[self.mapView.annotations count]]; 
    for (int i = 1; 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]; 
     } 
    } 

[self.mapView removeAnnotations:annotationsToRemove]; 
8

kürzesten Weg Löschen aller Anmerkungen und Beibehalten der MKUserLocation-Klassenannotation

[self.mapView removeAnnotations:self.mapView.annotations]; 
+0

Dies wird den Benutzer Standort entfernen – nielsbot

0

Für Swift 3,0

for annotation in self.mapView.annotations { 
    if let _ = annotation as? MKUserLocation { 
     // keep the user location 
    } else { 
     self.mapView.removeAnnotation(annotation) 
    } 
} 
+0

empfängt FehlermeldungType '[MGLAnnotation]?' entspricht nicht dem Protokoll 'Sequenz' –