How to delete all Annotations on a MKMapView

2019-01-16 05:01发布

Is there a simple way to delete all the annotations on a map without iterating through all the displayed annotations in Objective-c?

8条回答
再贱就再见
2楼-- · 2019-01-16 05:40

Swift 2.0 Simple and the best:

mapView.removeAnnotations(mapView.annotations)
查看更多
Anthone
3楼-- · 2019-01-16 05:51

Here is the simplest way to do that:

-(void)removeAllAnnotations
{
  //Get the current user location annotation.
  id userAnnotation=mapView.userLocation;

  //Remove all added annotations
  [mapView removeAnnotations:mapView.annotations]; 

  // Add the current user location annotation again.
  if(userAnnotation!=nil)
  [mapView addAnnotation:userAnnotation];
}
查看更多
叼着烟拽天下
4楼-- · 2019-01-16 05:51

Here's how to remove all annotations except the user location, written out explicitly because I imagine I will come looking for this answer again:

NSMutableArray *locs = [[NSMutableArray alloc] init];
for (id <MKAnnotation> annot in [mapView annotations])
{
    if ( [annot isKindOfClass:[ MKUserLocation class]] ) {
    }
    else {
        [locs addObject:annot];
    }
}
[mapView removeAnnotations:locs];
[locs release];
locs = nil;
查看更多
时光不老,我们不散
5楼-- · 2019-01-16 05:54

You do not need to save any reference to user location. All that is needed is:

[mapView removeAnnotations:mapView.annotations]; 

And as long as you have mapView.showsUserLocation set to YES, you will still have user location on the map. Settings this property to YES basically asks the map view to start updating and fetching user location, to to show it on the map. From the MKMapView.h comments:

// Set to YES to add the user location annotation to the map and start updating its location
查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-16 05:55

Swift 3

if let annotations = self.mapView.annotations {
    self.mapView.removeAnnotations(annotations)
}
查看更多
劳资没心,怎么记你
7楼-- · 2019-01-16 05:56

Yes, here is how

[mapView removeAnnotations:mapView.annotations]

However the previous line of code will remove all map annotations "PINS" from the map, including the user location pin "Blue Pin". To remove all map annotations and keep the user location pin on the map, there are two possible ways to do that

Example 1, retain the user location annotation, remove all pins, add the user location pin back, but there is a flaw with this approach, it will cause the user location pin to blink on the map, due to removing the pin then adding it back

- (void)removeAllPinsButUserLocation1 
{
    id userLocation = [mapView userLocation];
    [mapView removeAnnotations:[mapView annotations]];

    if ( userLocation != nil ) {
        [mapView addAnnotation:userLocation]; // will cause user location pin to blink
    }
}

Example 2, I personally prefer to avoid removing the location user pin in the first place,

- (void)removeAllPinsButUserLocation2
{
    id userLocation = [mapView userLocation];
    NSMutableArray *pins = [[NSMutableArray alloc] initWithArray:[mapView annotations]];
    if ( userLocation != nil ) {
        [pins removeObject:userLocation]; // avoid removing user location off the map
    }

    [mapView removeAnnotations:pins];
    [pins release];
    pins = nil;
}
查看更多
登录 后发表回答