【问题标题】:How do I remove all annotations from MKMapView except the user location annotation?如何从 MKMapView 中删除除用户位置注释之外的所有注释?
【发布时间】:2012-06-07 13:39:17
【问题描述】:

我使用removeAnnotationsmapView 中删除我的注释,但同样删除用户位置ann。我该如何防止这种情况发生,或者如何让用户 ann 重新查看?

NSArray *annotationsOnMap = mapView.annotations;
        [mapView removeAnnotations:annotationsOnMap];

【问题讨论】:

    标签: ios xcode mkmapview mkannotation userlocation


    【解决方案1】:

    更新:

    当我尝试使用 iOS 9 SDK 时,不再删除用户注释。你可以简单地使用

    mapView.removeAnnotations(mapView.annotations)
    

    历史答案(适用于 iOS 9 之前在 iOS 上运行的应用):

    试试这个:

    NSMutableArray * annotationsToRemove = [ mapView.annotations mutableCopy ] ;
    [ annotationsToRemove removeObject:mapView.userLocation ] ;
    [ mapView removeAnnotations:annotationsToRemove ] ;
    

    编辑:Swift 版本

    let annotationsToRemove = mapView.annotations.filter { $0 !== mapView.userLocation }
    mapView.removeAnnotations( annotationsToRemove )
    

    【讨论】:

    • 谢谢!这正是我所需要的!
    • 也感谢我 :)
    • 我试过改用这个:self.mapView.viewForAnnotation(annotation!)?.hidden = true 但是当我更改地图区域时它给了我奇怪的错误。你的方法更好,谢谢!
    • 我实现了这段代码,但我得到一个致命错误:意外发现 nil。我检查了 annotationsToRemove 计数是否大于一。我不知道为什么会收到此错误。请帮帮我。
    • 你必须看看哪一行失败了
    【解决方案2】:

    从地图中清除所有注释:

    [self.mapView removeAnnotations:[self.mapView annotations]];
    

    从 Mapview 中删除指定的注释

     for (id <MKAnnotation> annotation in self.mapView.annotations)
    {
        if (![annotation isKindOfClass:[MKUserLocation class]])
        {
                  [self.mapView removeAnnotation:annotation];   
        }
    
    }
    

    希望对你有所帮助。

    【讨论】:

    • 这个答案比公认的要简洁得多。
    【解决方案3】:

    对于 Swift,您可以简单地使用单线:

    mapView.removeAnnotations(mapView.annotations)
    

    编辑:正如 nielsbot 所提到的,它还会删除用户的位置注释,除非您像这样设置它:

    mapView.showsUserLocation = true
    

    【讨论】:

    • 这会删除所有注释,这不是 OP 想要的。
    • 行为发生了变化。
    【解决方案4】:

    如果您的用户位置属于MKUserLocation 类,请使用isKindOfClass 以避免删除用户位置注释。

    if (![annotation isKindOfClass:[MKUserLocation class]]) {
    
    }
    

    否则,您可以设置一个标志来识别– mapView:viewForAnnotation: 中的注释类型。

    【讨论】:

      【解决方案5】:

      Swift 4.2 或更高版本

      在添加注释之前添加这一行

      mapView.removeAnnotations(mapView.annotations.filter { $0 !== mapView.userLocation })
      

      【讨论】:

      • 最有效的答案。
      【解决方案6】:

      一些NSPredicate 过滤器怎么样?

      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"className != %@", NSStringFromClass(MKUserLocation.class)];
      NSArray *nonUserAnnotations = [self.mapView.annotations filteredArrayUsingPredicate:predicate];
      [self.mapView removeAnnotations:nonUserAnnotations];
      

      有了 NSPredicate 过滤器,生活总是更美好

      【讨论】:

        【解决方案7】:

        Swift 4.1 中:

        通常,如果您不想删除 MKUserLocation 注释,您可以简单地运行:

        self.mapView.removeAnnotations(self.annotations).

        默认情况下,此方法不会从 annotations 列表中删除 MKUserLocation 注释。

        但是,如果您出于任何其他原因需要过滤除 MKUserLocation(请参阅下面的 annotationsNoUserLocation 变量)之外的所有注释,例如以所有注释为中心,但MKUserLocation 注释,您可以在下面使用这个简单的扩展。

        extension MKMapView {
        
            var annotationsNoUserLocation : [MKAnnotation] {
                get {
                    return self.annotations.filter{ !($0 is MKUserLocation) }
                }
            }
        
            func showAllAnnotations() {
                self.showAnnotations(self.annotations, animated: true)
            }
        
            func removeAllAnnotations() {
                self.removeAnnotations(self.annotations)
            }
        
            func showAllAnnotationsNoUserLocation() {
                self.showAnnotations(self.annotationsNoUserLocation, animated: true)
            }
        
        }
        

        【讨论】:

          【解决方案8】:

          你好,试试这个,我从这段代码中得到了解决方案:

           NSMutableArray*listRemoveAnnotations = [[NSMutableArray alloc] init];
          [Mapview removeAnnotations:listRemoveAnnotations];
          
           [listRemoveAnnotations release];
          

          【讨论】:

          • 这没有回答问题。事实上这段代码没有任何作用——当-removeAnimations被调用时,listRemoveAnnotations是空的。
          猜你喜欢
          • 2011-03-02
          • 2015-01-13
          • 2012-04-27
          • 1970-01-01
          • 2011-01-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-22
          相关资源
          最近更新 更多