如何删除 MKMapView 上的所有注释

发布于 2024-09-05 13:07:06 字数 57 浏览 3 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

好久不见√ 2024-09-12 13:07:06

是的,方法如下

[mapView removeAnnotations:mapView.annotations]

但是,前一行代码将从中删除所有地图注释“PINS”
地图,包括用户位置图钉“蓝色图钉”。删除所有地图
注释并在地图上保留用户位置图钉,有两种
可能的方法来做到这一点

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

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

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

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

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

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;
}
睡美人的小仙女 2024-09-12 13:07:06

这是最简单的方法:

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

Here is the simplest way to do that:

-(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];
}
心碎的声音 2024-09-12 13:07:06

以下是如何删除除用户位置之外的所有注释,明确写出,因为我想我会再次寻找这个答案:

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;

Here's how to remove all annotations except the user location, written out explicitly because I imagine I will come looking for this answer again:

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;
注定孤独终老 2024-09-12 13:07:06

这与 Sandip 的答案非常相似,只是它不会重新添加用户位置,因此蓝点不会再次闪烁。

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

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

    [self.mapView removeAnnotations:annotations];
}

This is very similar to Sandip's answer, except that it doesn't re-add the user location so the blue dot doesn't blink on and off again.

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

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

    [self.mapView removeAnnotations:annotations];
}
[浮城] 2024-09-12 13:07:06

您不需要保存任何对用户位置的引用。所需要做的就是:

[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

You do not need to save any reference to user location. All that is needed is:

[mapView removeAnnotations:mapView.annotations]; 

And as long as you have mapView.showsUserLocation set to YES, you will still have user location on the map. Settings this property to YES basically asks the map view to start updating and fetching user location, to to show it on the map. From the MKMapView.h comments:

// Set to YES to add the user location annotation to the map and start updating its location
鹿港巷口少年归 2024-09-12 13:07:06

斯威夫特2.0
简单且最好:

mapView.removeAnnotations(mapView.annotations)

Swift 2.0
Simple and the best:

mapView.removeAnnotations(mapView.annotations)
百合的盛世恋 2024-09-12 13:07:06

斯威夫特版本:

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

Swift version:

func removeAllAnnotations() {
    let annotations = mapView.annotations.filter {
        $0 !== self.mapView.userLocation
    }
    mapView.removeAnnotations(annotations)
}
-残月青衣踏尘吟 2024-09-12 13:07:06

雨燕3

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

Swift 3

if let annotations = self.mapView.annotations {
    self.mapView.removeAnnotations(annotations)
}
半仙 2024-09-12 13:07:06

要删除一种类型的子类,您可以

mapView.removeAnnotations(mapView.annotations.filter({$0 is PlacesAnnotation}))

PlacesAnnotationMKAnnotation 的子类的情况下执行此操作

To remove one type of subclass you can do

mapView.removeAnnotations(mapView.annotations.filter({$0 is PlacesAnnotation}))

where PlacesAnnotation is a subclass of MKAnnotation

待"谢繁草 2024-09-12 13:07:06

以下是从 MKMapView 中删除所有标记以及所有路线(如果有)的函数:

 func removeAppleMapOverlays() {
    let overlays = self.appleMapView.overlays
    self.appleMapView.removeOverlays(overlays)
    let annotations = self.appleMapView.annotations.filter {
            $0 !== self.appleMapView.userLocation
        }
    self.appleMapView.removeAnnotations(annotations)
}

干杯

Here is the function to remove all markers as well as all routes (if any) from MKMapView:

 func removeAppleMapOverlays() {
    let overlays = self.appleMapView.overlays
    self.appleMapView.removeOverlays(overlays)
    let annotations = self.appleMapView.annotations.filter {
            $0 !== self.appleMapView.userLocation
        }
    self.appleMapView.removeAnnotations(annotations)
}

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文