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

后端 未结 8 1274
天命终不由人
天命终不由人 2020-12-24 06:19

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

8条回答
  •  天涯浪人
    2020-12-24 07:04

    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 )
    

提交回复
热议问题