2010-01-25 75 views
25

我想删除我的地图视图中的所有注释,而没有我的位置的蓝点。当我打电话时:如何从MKMapView中删除所有注释而不删除蓝点?

[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]]; 
     } 
    } 
+0

嘿垫,我试着用你的代码,它的工作原理,但由于某些原因,而不是在它能够在一个时间摆脱3或2时删除一个引脚。 ...那是怎么回事? – skinny123 2011-01-18 15:39:08

+0

尝试扭转互动。显然,删除一个意味着你的索引正在改变。从后面去除。 – chrism 2012-10-18 21:31:01

+0

可能的重复[如何从MKMapView中除去用户位置注释?](http://stackoverflow.com/questions/10865088/how-do-i-remove-all-annotations-from-mkmapview-except -the-user-location-annotati) – nielsbot 2015-02-18 04:44:34

回答

58

MKMapView documentation看,好像你有注释属性一起玩。它应该是非常简单的通过这个迭代,看看注解您有:

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

你也有userLocation属性,让你代表用户的位置标注。

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

希望这有助于

山姆

+0

嗨,谢谢你的诡计Sam !,我现在也用这种方法解决了: for(int i = 0; i <[mapView。注释计数];我++){ \t \t 如果([[mapView.annotations objectAtIndex:ⅰ] isKindOfClass:[MyAnnotationClass类]]){ [MapView的removeAnnotation:[mapView.annotations objectAtIndex:ⅰ]]; \t \t}再次 \t} ..thanks .. :) – Mat 2010-01-25 12:44:22

+0

这是做得太一个不错的办法 - 我删除所有注释,但你只去掉你已经添加了注解可能一个更安全的事情要做。好一个。 S – deanWombourne 2010-01-25 12:47:52

+1

垫,只是一个想法:不会跳过一个移动而不是被移除的注释吗?似乎之后的注释会得到被删除的指数,但是我无情地增加了指标。 – 2010-08-23 13:43:58

31

如果您:如果你去通过注释和记住所有这些不属于用户的位置,则可以使用removeAnnotations:方法,然后将其删除像快速简单的那样,有一种方法可以过滤MKUserLocation注释的数组。你可以将它传递给MKMapView的removeAnnotations:函数。

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

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

6
for (id annotation in map.annotations) { 
    NSLog(@"annotation %@", annotation); 

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

     [map removeAnnotation:annotation]; 
    } 
    } 

我修改这样

13

是不是很容易,只需做到以下几点:

//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]; 
+5

这是最好的答案! – 2012-05-14 00:19:07

+1

适合我,谢谢。 – 2012-07-17 23:13:18

1

更容易只是做到以下几点:

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]; 
8

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

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

这将删除用户位置 – nielsbot 2014-07-08 18:08:43

0

对于雨燕3.0

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

收到错误消息类型'[MGLAnnotation]?'不符合协议'序列' – 2017-12-02 17:45:31