如何删除 MKMapView 上的所有注释 [英] How to delete all Annotations on a MKMapView

查看:34
本文介绍了如何删除 MKMapView 上的所有注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以在不遍历 Objective-c 中所有显示的注释的情况下删除地图上的所有注释?

解决方案

是的,方法如下

[mapView removeAnnotations:mapView.annotations]

<块引用>

然而,前一行代码将从中删除所有地图注释PINS"地图,包括用户位置图钉蓝图钉".删除所有地图注释并在地图上保留用户位置图钉,有两个可能的方法来做到这一点

例1,保留用户位置注释,移除所有pin,添加用户位置锁定,但这种方法有一个缺陷,它由于删除,将导致用户位置图钉在地图上闪烁引脚然后将其添加回来

- (void)removeAllPinsButUserLocation1{id userLocation = [mapView userLocation];[mapView removeAnnotations:[mapView annotations]];如果(用户位置!= nil){[mapView addAnnotation:userLocation];//将导致用户位置引脚闪烁}}

<块引用>

示例 2,我个人更喜欢避免删除位置用户 pin首先,

- (void)removeAllPinsButUserLocation2{id userLocation = [mapView userLocation];NSMutableArray *pins = [[NSMutableArray alloc] initWithArray:[mapView annotations]];如果(用户位置!= nil){[pins removeObject:userLocation];//避免从地图上移除用户位置}[mapView removeAnnotations:pins];[引脚释放];引脚 = 无;}

Is there a simple way to delete all the annotations on a map without iterating through all the displayed annotations in Objective-c?

解决方案

Yes, here is how

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

这篇关于如何删除 MKMapView 上的所有注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆