【问题标题】:How to delete all Annotations on a MKMapView如何删除 MKMapView 上的所有注释
【发布时间】:2011-03-02 22:15:07
【问题描述】:

有没有一种简单的方法可以删除地图上的所有注释,而无需遍历 Objective-c 中显示的所有注释?

【问题讨论】:

    标签: objective-c iphone-sdk-3.0 annotations mkmapview


    【解决方案1】:

    是的,方法如下

    [mapView removeAnnotations:mapView.annotations]
    

    但是,上一行代码将删除所有地图注释“PINS” 地图,包括用户定位销“蓝销”。删除所有地图 注释并在地图上保留用户位置图钉,有两个 可能的方法来做到这一点

    示例1,保留用户位置标注,去掉所有pin,添加 用户定位销,但这种方法有一个缺陷,它 将导致用户位置图钉在地图上闪烁,因为移除 引脚然后将其添加回来

    - (void)removeAllPinsButUserLocation1 
    {
        id userLocation = [mapView userLocation];
        [mapView removeAnnotations:[mapView annotations]];
    
        if ( userLocation != nil ) {
            [mapView addAnnotation:userLocation]; // will cause user location pin to blink
        }
    }
    

    示例 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;
    }
    

    【讨论】:

    • 这是否也会删除用户位置?如果我想删除除用户位置之外的所有注释怎么办?
    • 您不需要保存对用户位置的任何引用。请阅读下面的答案以获取更多信息。
    【解决方案2】:

    这是最简单的方法:

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

    【讨论】:

    • 很好的答案,比遍历所有这些要快得多,尤其是当您有多个注释时。
    • 删除注释然后将其添加回来,会导致图钉在地图上闪烁。对于某些应用程序来说可能没什么大不了的,但如果您不断地用新的注释更新地图,这可能会让用户感到厌烦。
    • 由于某种原因,我的 userLocation 注释总是使用这种方法消失。 Victor Van Hee 的解决方案对我有用。
    【解决方案3】:

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

    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;
    

    【讨论】:

    • 谢谢,这对我有用,复制和粘贴,删除 [locs release] 并将 mapView 更改为 _mapView。我在devfright.com/mkdirections-tutorial 上关注了一个很棒的 MKDirections 教程,并想在获得方向后移除图钉。我将该方法最后一行下方的代码添加到“清除路线”方法
    • 更新删除多个 - (IBAction)clearRoute:(UIBarButtonItem *)sender { self.destinationLabel.text = nil; self.distanceLabel.text = nil; self.steps.text = nil; [self.mapView removeOverlay:routeDetails.polyline]; NSMutableArray *locs = [[NSMutableArray alloc] init]; for (id annot in [_mapView annotations]) { if ( [annot isKindOfClass:[ MKUserLocation class]] ) { } else { [locs addObject:annot]; } } [_mapView removeAnnotations:locs]; [_mapView removeOverlays:_mapView.overlays]; }
    【解决方案4】:

    这与 Sandip 的回答非常相似,只是它不会重新添加用户位置,因此蓝点不会再次闪烁。

    -(void)removeAllAnnotations
    {
        id userAnnotation = self.mapView.userLocation;
    
        NSMutableArray *annotations = [NSMutableArray arrayWithArray:self.mapView.annotations];
        [annotations removeObject:userAnnotation];
    
        [self.mapView removeAnnotations:annotations];
    }
    

    【讨论】:

      【解决方案5】:

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

      [mapView removeAnnotations:mapView.annotations]; 
      

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

      // Set to YES to add the user location annotation to the map and start updating its location
      

      【讨论】:

      • 我最终使用了这种格式:[self.mapView.removeAnnotations(mapView.annotations)]
      【解决方案6】:

      Swift 版本:

      func removeAllAnnotations() {
          let annotations = mapView.annotations.filter {
              $0 !== self.mapView.userLocation
          }
          mapView.removeAnnotations(annotations)
      }
      

      【讨论】:

        【解决方案7】:

        斯威夫特 3

        if let annotations = self.mapView.annotations {
            self.mapView.removeAnnotations(annotations)
        }
        

        【讨论】:

          【解决方案8】:

          斯威夫特 2.0 简单而最好的:

          mapView.removeAnnotations(mapView.annotations)
          

          【讨论】:

            【解决方案9】:

            要删除一种类型的子类,您可以这样做

            mapView.removeAnnotations(mapView.annotations.filter({$0 is PlacesAnnotation}))
            

            其中PlacesAnnotationMKAnnotation 的子类

            【讨论】:

              【解决方案10】:

              这是从 MKMapView 中删除所有标记以及所有路线(如果有)的功能:

               func removeAppleMapOverlays() {
                  let overlays = self.appleMapView.overlays
                  self.appleMapView.removeOverlays(overlays)
                  let annotations = self.appleMapView.annotations.filter {
                          $0 !== self.appleMapView.userLocation
                      }
                  self.appleMapView.removeAnnotations(annotations)
              }
              

              干杯

              【讨论】:

                猜你喜欢
                • 2012-06-07
                • 2012-04-27
                • 2011-01-09
                • 1970-01-01
                • 2010-11-25
                • 1970-01-01
                • 2016-02-29
                • 1970-01-01
                • 2016-05-21
                相关资源
                最近更新 更多