2012-06-02 66 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總是更好的過濾