2010-06-12 22 views

回答

231

是的,這裏是如何

[mapView removeAnnotations:mapView.annotations] 

然而代碼前行將從 地圖上刪除所有地圖註釋「銷」,包括用戶定位銷「藍針」。要刪除所有地圖 註釋和保持用戶定位銷在地圖上,有兩種 可能的方式來做到這一點

例1,保留用戶的位置標註,刪除所有引腳,加 用戶定位銷回,但這種方法的一個缺陷,它 將使用戶位置銷閃爍在地圖上時,由於去除 銷然後把它加回到

- (void)removeAllPinsButUserLocation1 
{ 
    id userLocation = [mapView userLocation]; 
    [mapView removeAnnotations:[mapView annotations]]; 

    if (userLocation != nil) { 
     [mapView addAnnotation:userLocation]; // will cause user location pin to blink 
    } 
} 

例2中,我個人更喜歡避免ID首先取出位置的用戶銷 ,

- (void)removeAllPinsButUserLocation2 
{ 
    id userLocation = [mapView userLocation]; 
    NSMutableArray *pins = [[NSMutableArray alloc] initWithArray:[mapView annotations]]; 
    if (userLocation != nil) { 
     [pins removeObject:userLocation]; // avoid removing user location off the map 
    } 

    [mapView removeAnnotations:pins]; 
    [pins release]; 
    pins = nil; 
} 
+1

這是否刪除用戶位置呢?如果我想刪除用戶位置以外的所有註釋,該怎麼辦? – 2010-06-13 22:17:29

+1

您不需要保存對用戶位置的任何引用。請閱讀我的答案以獲取更多信息。 – 2014-04-15 19:55:19

17

以下是如何刪除所有註釋除了用戶的位置,明確地寫出來,因爲我想我一定會回來尋找這個答案:

NSMutableArray *locs = [[NSMutableArray alloc] init]; 
for (id <MKAnnotation> annot in [mapView annotations]) 
{ 
    if ([annot isKindOfClass:[ MKUserLocation class]]) { 
    } 
    else { 
     [locs addObject:annot]; 
    } 
} 
[mapView removeAnnotations:locs]; 
[locs release]; 
locs = nil; 
+0

謝謝,這適用於我複製和粘貼並刪除[locs發佈]並將mapView更改爲_mapView。我在這裏http://www.devfright.com/mkdirections-tutorial上爲MKDirections提供了一個很好的教程,並且想要在獲取路線後刪除這個引腳。我將該方法的最後一行下方的代碼添加到「清除路由」方法 – Hblegg 2014-03-18 05:37:25

+0

update remove multiple - (IBAction)clearRoute :(UIBarButtonItem *)sender self.destinationLabel.text = nil; self.distanceLabel.text = nil; self.steps.text = nil; [self.mapView removeOverlay:routeDetails.polyline]; NSMutableArray * locs = [[NSMutableArray alloc] init]; 爲(ID ANNOT在[_mapView註釋]) { 如果([ANNOT isKindOfClass:[MKUserLocation類]]){ } 否則{ [LOCS ADDOBJECT:ANNOT]; } } [_mapView removeAnnotations:locs]; [_mapView removeOverlays:_mapView.overlays]; } – Hblegg 2014-03-18 19:56:53

35

下面是做到這一點的最簡單的方法:

-(void)removeAllAnnotations 
{ 
    //Get the current user location annotation. 
    id userAnnotation=mapView.userLocation; 

    //Remove all added annotations 
    [mapView removeAnnotations:mapView.annotations]; 

    // Add the current user location annotation again. 
    if(userAnnotation!=nil) 
    [mapView addAnnotation:userAnnotation]; 
} 
+0

好的答案,比迭代遍歷所有這些要快得多,特別是如果您有多個註釋。 – 2011-01-15 11:10:02

+6

刪除註釋並將其添加回來會導致地圖上的引腳閃爍。對於某些應用程序可能不是什麼大問題,但如果您持續使用新註釋更新地圖,則可能會對用戶感到煩惱。 – RocketMan 2011-08-03 12:51:21

+0

出於某種原因,我的userLocation註釋總是使用此方法消失。 Victor Van Hee的解決方案適用於我。 – 2011-12-16 17:12:26

13

這是非常相似與S andip的回答,除了它不重新添加用戶位置,所以藍點不會再閃爍。

-(void)removeAllAnnotations 
{ 
    id userAnnotation = self.mapView.userLocation; 

    NSMutableArray *annotations = [NSMutableArray arrayWithArray:self.mapView.annotations]; 
    [annotations removeObject:userAnnotation]; 

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

愛無循環的解決方案,grreeeeeat! – 2014-02-21 22:15:05

11

您不需要保存對用戶位置的任何引用。所有這一切需要的是:

[mapView removeAnnotations:mapView.annotations]; 

而且只要你有mapView.showsUserLocation設置爲YES,您仍然可以在地圖上用戶的位置。將此屬性設置爲YES基本上會要求地圖視圖開始更新並獲取用戶位置,以便在地圖上顯示它。從MKMapView.h評論:

// Set to YES to add the user location annotation to the map and start updating its location 
+0

我結束了使用這種格式:[self.mapView.removeAnnotations(mapView.annotations)] – 2015-07-06 17:25:59

5

斯威夫特版本:

func removeAllAnnotations() { 
    let annotations = mapView.annotations.filter { 
     $0 !== self.mapView.userLocation 
    } 
    mapView.removeAnnotations(annotations) 
} 
1

雨燕2.0 簡單和最好的:

mapView.removeAnnotations(mapView.annotations) 
2

斯威夫特3

if let annotations = self.mapView.annotations { 
    self.mapView.removeAnnotations(annotations) 
}