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

后端 未结 8 1273
天命终不由人
天命终不由人 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:23

    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.

    0 讨论(0)
  • 2020-12-24 07:27

    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

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