开发者

How to remove all annotations from MKMapView without removing the blue dot?

开发者 https://www.devze.com 2022-12-17 06:16 出处:网络
I would like to remove all annotations from my mapview without the blue dot of my position.When I call:

I would like to remove all annotations from my mapview without the blue dot of my position. When I call:

[mapView removeAnnotations:mapView.annotations];

all annotations are removed.

In which way can I check (like a for loop on all the annotations) if the annotation is not the blue dot annotation?

EDIT (I've solve开发者_JAVA技巧d with this):

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


Looking at the MKMapView documentation, it seems like you have the annotations property to play with. It should be pretty simple to iterate through this and see what annotations you have :

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

You also have the userLocation property which gives you the annotation representing the user's location. If you go through the annotations and remember all of them which are not the user location, you can then remove them using the removeAnnotations: method :

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

Hope this helps,

Sam


If you like quick and simple, there's a way to filter an array of the MKUserLocation annotation. You can pass this into MKMapView's removeAnnotations: function.

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

I assume this is pretty much the same as the manual filters posted above, except using a predicate to do the dirty work.


Isn't it easier to just do the following:

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


shortest way to clean all annotations and preserving MKUserLocation class annotation

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


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

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

        [map removeAnnotation:annotation];
    }
    }

i modified like this


it easier to just do the following:

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


For Swift 3.0

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号