【问题标题】:How to remove all annotations from MKMapView without removing the blue dot?如何在不删除蓝点的情况下从 MKMapView 中删除所有注释?
【发布时间】:2011-01-09 02:01:30
【问题描述】:

我想从我的地图视图中删除所有没有我位置蓝点的注释。当我打电话时:

[mapView removeAnnotations:mapView.annotations];

所有注释都被删除。

如果注释不是蓝点注释,我可以通过哪种方式检查(例如所有注释上的 for 循环)?

编辑(我已经解决了):

for (int i =0; i < [mapView.annotations count]; i++) { 
    if ([[mapView.annotations objectAtIndex:i] isKindOfClass:[MyAnnotationClass class]]) {                      
         [mapView removeAnnotation:[mapView.annotations objectAtIndex:i]]; 
       } 
    }

【问题讨论】:

标签: iphone mkmapview mkannotation


【解决方案1】:

查看MKMapView documentation,您似乎可以使用 annotations 属性。遍历它并查看您有哪些注释应该非常简单:

for (id annotation in myMap.annotations) {
    NSLog(@"%@", annotation);
}

您还拥有userLocation 属性,它为您提供代表用户位置的注释。如果您仔细阅读注释并记住所有不是用户位置的注释,则可以使用removeAnnotations: 方法将其删除:

NSInteger toRemoveCount = myMap.annotations.count;
NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:toRemoveCount];
for (id annotation in myMap.annotations)
    if (annotation != myMap.userLocation)
        [toRemove addObject:annotation];
[myMap removeAnnotations:toRemove];

希望对你有帮助,

山姆

【讨论】:

  • 嗨,谢谢你的把戏 Sam!,我现在也用这种方式解决了:for (int i =0; i
  • 这也是一个很好的方法 - 我正在删除所有注释,但您只删除了您添加的注释,这可能是更安全的做法。好东西。 S
  • 垫子,只是一个想法:这不会跳过一个被移动的注释来代替被删除的注释吗?似乎注释之后会得到被删除的索引,但 i 计数器无情地增加。
  • 每个 jlballes,最后一行应该是 [myMap removeAnnotations:toRemove];
  • [map removeAnnotations:[map annotations]];应该做的伎俩
【解决方案2】:

如果您喜欢快速和简单,可以使用一种过滤 MKUserLocation 注释数组的方法。您可以将其传递给 MKMapView 的 removeAnnotations: 函数。

 [_mapView.annotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"!(self isKindOfClass: %@)", [MKUserLocation class]]];

我认为这与上面发布的手动过滤器几乎相同,除了使用谓词来完成肮脏的工作。

【讨论】:

    【解决方案3】:

    做以下事情不是更容易吗:

    //copy your annotations to an array
        NSMutableArray *annotationsToRemove = [[NSMutableArray alloc] initWithArray: mapView.annotations]; 
    //Remove the object userlocation
        [annotationsToRemove removeObject: mapView.userLocation]; 
     //Remove all annotations in the array from the mapView
        [mapView removeAnnotations: annotationsToRemove];
        [annotationsToRemove release];
    

    【讨论】:

      【解决方案4】:

      清理所有注释并保留 MKUserLocation 类注释的最短方法

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

      【讨论】:

      • 这将删除用户位置
      【解决方案5】:
      for (id annotation in map.annotations) {
          NSLog(@"annotation %@", annotation);
      
          if (![annotation isKindOfClass:[MKUserLocation class]]){
      
              [map removeAnnotation:annotation];
          }
          }
      

      我是这样修改的

      【讨论】:

        【解决方案6】:

        执行以下操作更容易:

        NSMutableArray *annotationsToRemove = [NSMutableArray arrayWithCapacity:[self.mapView.annotations count]];
            for (int i = 1; i < [self.mapView.annotations count]; i++) {
                if ([[self.mapView.annotations objectAtIndex:i] isKindOfClass:[AddressAnnotation class]]) {
                    [annotationsToRemove addObject:[self.mapView.annotations objectAtIndex:i]];
                    [self.mapView removeAnnotations:annotationsToRemove];
                }
            }
        
        [self.mapView removeAnnotations:annotationsToRemove];
        

        【讨论】:

          【解决方案7】:

          对于 Swift 3.0

          for annotation in self.mapView.annotations {
              if let _ = annotation as? MKUserLocation {
                 // keep the user location
              } else {
                 self.mapView.removeAnnotation(annotation)
              }
          }
          

          【讨论】:

          • 收到错误消息类型“[MGLAnnotation]?”不符合协议'Sequence'
          猜你喜欢
          • 2011-03-02
          • 2012-06-07
          • 2012-04-27
          • 2021-05-10
          • 2012-11-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-28
          相关资源
          最近更新 更多