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

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

问题描述

有没有一种简单的方法来删除一个地图上所有的注释,而无需通过所有显示的注释在Objective-C迭代?


解决方案

是的,这里是如何

  [图形页面removeAnnotations:mapView.annotations]


  

然而code的previous线将删除所有地图标注销
  地图上,包括用户的位置针蓝针。要删除所有地图
  注释和保持用户位置销在地图上,有两个
  可能的方式来做到这一点。


  
  

例1,保留用户的位置标注,删除所有引脚,加
  用户定位销回,但这种方法的缺陷,那
  将使用户位置销闪烁在地图上,由于除去
  销然后添加回


   - (无效)removeAllPinsButUserLocation1
{
    ID userLocation = [图形页面userLocation]
    [图形页面removeAnnotations:[注解MapView类];    如果(userLocation!=无){
        [图形页面addAnnotation:userLocation]; //会导致用户定位销闪烁
    }
}


  

例2,我个人preFER以免吸位置的用户PIN
  首先,


   - (无效)removeAllPinsButUserLocation2
{
    ID userLocation = [图形页面userLocation]
    NSMutableArray里*销= [[NSMutableArray里的alloc] initWithArray:[注解MapView类];
    如果(userLocation!=无){
        [销的removeObject:userLocation]; //以免吸走用户位置从地图
    }    [图形页面removeAnnotations:引脚];
    [管脚上]
    销=零;
}

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天全站免登陆