2012-06-02 41 views

回答

81

更新:

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

mapView.removeAnnotations(mapView.annotations) 

历史答案(针对iOS的前9 iOS上运行的应用程序):

试试这个:

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

编辑:雨燕版

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

谢谢!这正是我需要的! –

+0

Thanx ..... Buddy – user968597

+0

也谢谢我:) –

3

如果您的用户的位置是一种类MKUserLocation的,使用isKindOfClass避免删除用户的位置标注。

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

} 

否则,你可以设置一个标志来识别一种注解的– mapView:viewForAnnotation:

+0

谢谢!我会记住这个 –

-1

嗨,试试这个我得到了解决方案从离子这段代码:

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

[listRemoveAnnotations release]; 
+0

这并不回答这个问题。实际上,这个代码没有效果 - 当调用'-removeAnimations'时''listRemoveAnnotations'是空的。 – nielsbot

19

要清除所有地图中的注释:

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

从MapView的

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

} 

希望这可以帮助您删除指定的注解。

+1

这个答案比公认的简洁得多。 – shapeare

6

对于斯威夫特,你可以简单地用一个班轮:

mapView.removeAnnotations(mapView.annotations) 

编辑:nielsbot提到它也将删除用户的位置标注,除非你已经设置它是这样的:

mapView.showsUserLocation = true 
+0

这将删除所有注释,这不是OP想要的注释。 – nielsbot

+0

谢谢,我刚刚更新了我的答案。 – raspi

+0

不要以为这是可行的。你试过了吗? – nielsbot

0

某些NSPredicate过滤器如何?

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

生活与NSPredicate总是更好的过滤