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

后端 未结 7 1001
青春惊慌失措
青春惊慌失措 2020-12-07 23:16

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

[mapView removeAnnotations:mapView.annotations];


        
相关标签:
7条回答
  • 2020-12-07 23:29

    shortest way to clean all annotations and preserving MKUserLocation class annotation

    [self.mapView removeAnnotations:self.mapView.annotations];
    
    0 讨论(0)
  • 2020-12-07 23:32

    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];
    
    0 讨论(0)
  • 2020-12-07 23:35

    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.

    0 讨论(0)
  • 2020-12-07 23:35
    for (id annotation in map.annotations) {
        NSLog(@"annotation %@", annotation);
    
        if (![annotation isKindOfClass:[MKUserLocation class]]){
    
            [map removeAnnotation:annotation];
        }
        }
    

    i modified like this

    0 讨论(0)
  • 2020-12-07 23:38

    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];
    
    0 讨论(0)
  • 2020-12-07 23:47

    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

    0 讨论(0)
提交回复
热议问题