40

I use removeAnnotations to remove my annotations from mapView but same it remove user location ann. How can I prevent this, or how to get user ann back to view?

NSArray *annotationsOnMap = mapView.annotations;
        [mapView removeAnnotations:annotationsOnMap];
nielsbot
  • 15,922
  • 4
  • 48
  • 73
Pavel Kaljunen
  • 1,291
  • 2
  • 26
  • 53

8 Answers8

98

Update:

When I tried with the iOS 9 SDK the user annotation is no longer removed. You can simply use

mapView.removeAnnotations(mapView.annotations)

Historical answer (for apps that run on iOS before iOS 9):

Try this:

NSMutableArray * annotationsToRemove = [ mapView.annotations mutableCopy ] ;
[ annotationsToRemove removeObject:mapView.userLocation ] ;
[ mapView removeAnnotations:annotationsToRemove ] ;

EDIT: Swift version

let annotationsToRemove = mapView.annotations.filter { $0 !== mapView.userLocation }
mapView.removeAnnotations( annotationsToRemove )
nielsbot
  • 15,922
  • 4
  • 48
  • 73
  • I've tried using this instead: self.mapView.viewForAnnotation(annotation!)?.hidden = true but it was giving me weird errors when I change map region. Yours is the better way, thank you! – user1019042 Jul 20 '16 at 02:26
  • I implemented this code but I get a fatal error: unexpectedly found nil. I checked whether annotationsToRemove counts greater than one. I have no idea why I am getting this error. Please help me. – Lenny1357 Sep 22 '16 at 19:11
  • you have to look and see what line is failed on – nielsbot Sep 23 '16 at 00:49
  • It's in the MapView.removeAnnotations line – Lenny1357 Sep 23 '16 at 08:32
  • Do you have any `!` in your code? How is your MapView variable defined? Change any `!` to `?` and then further modify your code so it compiles. The crash will probably be resolved. – nielsbot Sep 23 '16 at 18:32
21

To clear all the annotations from the map:

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

To remove specified annotations from Mapview

 for (id <MKAnnotation> annotation in self.mapView.annotations)
{
    if (![annotation isKindOfClass:[MKUserLocation class]])
    {
              [self.mapView removeAnnotation:annotation];   
    }

}

Hope this may help you.

Aswathy Bose
  • 4,279
  • 4
  • 32
  • 44
7

For Swift you can simply use a one-liner:

mapView.removeAnnotations(mapView.annotations)

Edit: As nielsbot mentioned it will also remove the user's location annotation unless you have set it up like this:

mapView.showsUserLocation = true
Thilo
  • 989
  • 1
  • 17
  • 27
3

If your user location is kind of class of MKUserLocation, use isKindOfClass to avoid removing user location annotation.

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

}

Else you can set a flag to recognize the kind of your annotations in – mapView:viewForAnnotation:.

bradley
  • 648
  • 5
  • 6
1

Swift 4.2 or later

Add this line before adding the annotations

mapView.removeAnnotations(mapView.annotations.filter { $0 !== mapView.userLocation })
SomeDutchGuy
  • 2,249
  • 4
  • 16
  • 42
0

How about some NSPredicate filter?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"className != %@", NSStringFromClass(MKUserLocation.class)];
NSArray *nonUserAnnotations = [self.mapView.annotations filteredArrayUsingPredicate:predicate];
[self.mapView removeAnnotations:nonUserAnnotations];

Life is always better with NSPredicate filter

Laszlo
  • 2,803
  • 2
  • 28
  • 33
0

In Swift 4.1:

Normally if you don't want to remove your MKUserLocation annotation you can simply run:

self.mapView.removeAnnotations(self.annotations).

This method by default does not remove the MKUserLocation annotation from the annotations list.

However if you need to filter out all annotations except the MKUserLocation (see annotationsNoUserLocation variable below) for any other reason, like centering on all the annotations but the MKUserLocation annotation you can use this simple extension below.

extension MKMapView {

    var annotationsNoUserLocation : [MKAnnotation] {
        get {
            return self.annotations.filter{ !($0 is MKUserLocation) }
        }
    }

    func showAllAnnotations() {
        self.showAnnotations(self.annotations, animated: true)
    }

    func removeAllAnnotations() {
        self.removeAnnotations(self.annotations)
    }

    func showAllAnnotationsNoUserLocation() {
        self.showAnnotations(self.annotationsNoUserLocation, animated: true)
    }

}
madx
  • 6,723
  • 4
  • 55
  • 59
-1

Hi try this i got the solution from this code:

 NSMutableArray*listRemoveAnnotations = [[NSMutableArray alloc] init];
[Mapview removeAnnotations:listRemoveAnnotations];

 [listRemoveAnnotations release];
madesh
  • 11
  • 5
  • this doesn't answer the question. In fact this code as no effect--`listRemoveAnnotations` is empty when `-removeAnimations` is called. – nielsbot May 05 '13 at 00:41