2010-06-12 33 views

Trả lời

231

Vâng, đây là cách

[mapView removeAnnotations:mapView.annotations] 

Tuy nhiên dòng trước đó của mã sẽ loại bỏ tất cả các chú thích bản đồ "PINS" từ bản đồ, bao gồm cả pin vị trí người dùng "Blue Pin". Để loại bỏ tất cả các bản đồ chú thích và giữ cho pin sử dụng vị trí trên bản đồ, có hai cách có thể để làm điều đó

Ví dụ 1, giữ lại vị trí người dùng chú thích, loại bỏ tất cả chân, thêm các vị trí người dùng pin trở lại, nhưng có một lỗ hổng với cách tiếp cận này, nó sẽ làm cho pin vị trí người dùng nhấp nháy trên bản đồ, do loại bỏ pin sau đó thêm nó trở lại

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

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

Ví dụ 2, cá nhân tôi thích để avo id tháo pin vị trí người dùng ở nơi đầu tiên,

- (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

Điều này loại bỏ người dùng vị trí quá? nếu tôi muốn xóa tất cả chú thích bên cạnh vị trí của người dùng thì sao? –

+1

Bạn không cần phải lưu bất kỳ tham chiếu nào đến vị trí của người dùng. Đọc câu trả lời của tôi bên dưới để biết thêm thông tin. –

17

Dưới đây là làm thế nào để loại bỏ tất cả các chú thích, ngoại trừ vị trí người sử dụng, được viết ra một cách rõ ràng bởi vì tôi tưởng tượng tôi sẽ đến tìm kiếm câu trả lời này một lần nữa:

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

Cảm ơn bạn, Điều này đã giúp tôi sao chép và dán và xóa [locs release] và thay đổi mapView thành _mapView. Tôi đã làm theo một hướng dẫn tuyệt vời cho MKDirections tại đây http://www.devfright.com/mkdirections-tutorial và muốn tháo ghim sau khi nhận chỉ đường. Tôi đã thêm mã bên dưới dòng cuối cùng của phương thức đó vào 'clear route'method – Hblegg

+0

update remove multiple - (IBAction) clearRoute: (UIBarButtonItem *) người gửi { self.destinationLabel.text = nil; self.distanceLabel.text = nil; self.steps.text = nil; [self.mapView removeOverlay: routeDetails.polyline]; NSMutableArray * locs = [[NSMutableArray alloc] init]; cho (id ANNOT trong [_mapView chú thích]) { if ([ANNOT isKindOfClass: [MKUserLocation lớp]]) {} else { [Locs AddObject: ANNOT]; } } [_mapView removeAnnotations: locs]; [_mapView removeOverlays: _mapView.overlays]; } – Hblegg

35

Dưới đây là cách đơn giản nhất để làm điều đó:

-(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

Câu trả lời hay, nhanh hơn nhiều so với lặp lại thông qua tất cả chúng, đặc biệt nếu bạn có nhiều chú thích. –

+6

Xóa chú thích rồi thêm lại chú thích, làm cho ghim nhấp nháy trên bản đồ. Có thể không phải là một việc lớn đối với một số ứng dụng, nhưng nó có thể gây khó chịu cho người dùng nếu bạn luôn cập nhật bản đồ bằng các chú thích mới. – RocketMan

+0

Vì lý do nào đó, chú thích userLocation của tôi luôn biến mất khi sử dụng phương pháp này. Giải pháp của Victor Van Hee làm việc cho tôi. –

13

này rất giống với S câu trả lời của andip, ngoại trừ việc nó không thêm lại vị trí của người dùng để dấu chấm màu xanh lam không nhấp nháy và bật lại.

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

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

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

yêu các giải pháp mà không có vòng lặp, grreeeeeat !!! –

11

Bạn không cần lưu bất kỳ tham chiếu nào đến vị trí của người dùng. Tất cả những gì cần thiết là:

[mapView removeAnnotations:mapView.annotations]; 

Và chừng nào bạn có mapView.showsUserLocation thiết lập để YES, bạn vẫn sẽ có vị trí người dùng trên bản đồ. Cài đặt thuộc tính này thành YES về cơ bản yêu cầu chế độ xem bản đồ bắt đầu cập nhật và tìm nạp vị trí của người dùng, để hiển thị nó trên bản đồ. Từ MKMapView.h nhận xét:

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

Tôi đã sử dụng định dạng sau: [self.mapView.removeAnnotations (mapView.annotations)] –

5

phiên bản Swift:

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

Swift 2.0 đơn giản và tốt nhất:

mapView.removeAnnotations(mapView.annotations) 
2

Swift 3

if let annotations = self.mapView.annotations { 
    self.mapView.removeAnnotations(annotations) 
} 
Các vấn đề liên quan