How do I remove all annotations from MKMapView except the user location annotation?

廉价感情. 提交于 2019-11-28 19:39:53

问题


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

回答1:


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 )



回答2:


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.




回答3:


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



回答4:


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:.




回答5:


Swift 4.2 or later

Add this line before adding the annotations

mapView.removeAnnotations(mapView.annotations.filter { $0 !== mapView.userLocation })



回答6:


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




回答7:


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)
    }

}



回答8:


Hi try this i got the solution from this code:

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

 [listRemoveAnnotations release];


来源:https://stackoverflow.com/questions/10865088/how-do-i-remove-all-annotations-from-mkmapview-except-the-user-location-annotati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!