2010-01-25 76 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