如何删除MKMapView上的所有注释

kev*_*oza 98 annotations objective-c iphone-sdk-3.0 mkmapview

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

Roc*_*Man 243

是的,这是怎么回事

[mapView removeAnnotations:mapView.annotations]
Run Code Online (Sandbox Code Playgroud)

但是,前一行代码将从地图中删除所有地图注释"PINS",包括用户位置引脚"Blue Pin".要删除所有地图注释并将用户位置图钉保留在地图上,有两种可能的方法

例1,保留用户位置注释,删除所有引脚,添加用户位置引脚,但这种方法存在缺陷,它会导致用户定位引脚在地图上闪烁,因为移除引脚然后添加它背部

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

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

例2,我个人更愿意避免首先删除位置用户引脚,

- (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;
}
Run Code Online (Sandbox Code Playgroud)


小智 36

这是最简单的方法:

-(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];
}
Run Code Online (Sandbox Code Playgroud)

  • 删除注释然后将其添加回来会导致图钉在地图上闪烁.某些应用程序可能不是什么大问题,但如果您不断使用新注释更新地图,则可能会让用户感到烦恼. (6认同)

Vic*_*Hee 17

以下是如何删除除用户位置之外的所有注释,明确写出,因为我想我会再来寻找这个答案:

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;
Run Code Online (Sandbox Code Playgroud)


Chr*_*ris 13

这与Sandip的答案非常相似,不同之处在于它不会重新添加用户位置,因此蓝点不会再次闪烁.

-(void)removeAllAnnotations
{
    id userAnnotation = self.mapView.userLocation;

    NSMutableArray *annotations = [NSMutableArray arrayWithArray:self.mapView.annotations];
    [annotations removeObject:userAnnotation];

    [self.mapView removeAnnotations:annotations];
}
Run Code Online (Sandbox Code Playgroud)


Avi*_*oss 11

您无需保存对用户位置的任何引用.所需要的只是:

[mapView removeAnnotations:mapView.annotations]; 
Run Code Online (Sandbox Code Playgroud)

只要您mapView.showsUserLocation设置为YES,您仍将在地图上拥有用户位置.设置此属性YES基本上要求地图视图开始更新和获取用户位置,以便在地图上显示它.来自MKMapView.h评论:

// Set to YES to add the user location annotation to the map and start updating its location
Run Code Online (Sandbox Code Playgroud)


Mas*_*lko 7

Swift 2.0 简单且最好:

mapView.removeAnnotations(mapView.annotations)
Run Code Online (Sandbox Code Playgroud)


Kos*_*awa 6

Swift版本:

func removeAllAnnotations() {
    let annotations = mapView.annotations.filter {
        $0 !== self.mapView.userLocation
    }
    mapView.removeAnnotations(annotations)
}
Run Code Online (Sandbox Code Playgroud)


小智 6

斯威夫特3

if let annotations = self.mapView.annotations {
    self.mapView.removeAnnotations(annotations)
}
Run Code Online (Sandbox Code Playgroud)