objective-c - 如何删除 MKMapView 上的所有注释

有没有一种简单的方法可以删除 map 上的所有注释,而无需遍历 Objective-c 中显示的所有注释?

最佳答案

是的,方法如下

[mapView removeAnnotations:mapView.annotations]

However the previous line of code will remove all map annotations "PINS" from the map, including the user location pin "Blue Pin". To remove all map annotations and keep the user location pin on the map, there are two possible ways to do that

Example 1, retain the user location annotation, remove all pins, add the user location pin back, but there is a flaw with this approach, it will cause the user location pin to blink on the map, due to removing the pin then adding it back

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

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

Example 2, I personally prefer to avoid removing the location user pin in the first place,

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

https://stackoverflow.com/questions/3027392/

相关文章:

ios - 如何防止 UINavigationBar 在 iOS 7 中覆盖 View 顶部?

objective-c - iOS:比较两个日期

ios - 如何屏蔽 UIImageView?

objective-c - iPad 纵向和横向模式的尺寸调整类

objective-c - 获取对 iOS 应用程序中最顶层 View /窗口的引用

iphone - 如何从 UITableViewCell 获取 UITableView?

ios - 带有固定部分标题的 UITableView

objective-c - 更改 UIButton 文本

iphone - 如何获得精确的时间,例如在 Objective-C 中以毫秒为单位?

ios - 了解 NSRunLoop