简体   繁体   English

如何在 swift 2 中删除所有地图注释

[英]How do I remove all map annotations in swift 2

I had working code to remove all map annotations with a button, but after my update to xcode 7 I am running into the error:我有工作代码可以使用按钮删除所有地图注释,但是在更新到 xcode 7 后,我遇到了错误:

Type 'MKAnnotation' does not conform to protocol 'SequenceType'类型 'MKAnnotation' 不符合协议 'SequenceType'

if let annotations = (self.mapView.annotations as? MKAnnotation){
    for _annotation in annotations {
        if let annotation = _annotation as? MKAnnotation {
            self.mapView.removeAnnotation(annotation)
        }
    }
}

In Swift 2 annotations is declared as non optional array [MKAnnotation] so you can easily write在 Swift 2 中, annotations被声明为非可选数组[MKAnnotation]因此您可以轻松编写

let allAnnotations = self.mapView.annotations
self.mapView.removeAnnotations(allAnnotations)

without any type casting.没有任何类型转换。

self.mapView.removeAnnotations(self.mapView.annotations)

If you don't want remove user location.如果您不想删除用户位置。

self.mapView.annotations.forEach {
  if !($0 is MKUserLocation) {
    self.mapView.removeAnnotation($0)
  }
}

Note: Objective-C now have generics, it is no longer necessary cast the elements of 'annotations' array.注意:Objective-C 现在有了泛型,不再需要强制转换 'annotations' 数组的元素。

SWIFT 5快速 5

If you don't want to remove user location mark:如果您不想删除用户位置标记:

let annotations = mapView.annotations.filter({ !($0 is MKUserLocation) })
mapView.removeAnnotations(annotations)

The issue is that there are two methods.问题是有两种方法。 One is removeAnnotation which takes an MKAnnotation object, and the other is removeAnnotations which takes an array of MKAnnotations, note the "s" at the end of one and not the other.一个是 removeAnnotation,它接受一个 MKAnnotation 对象,另一个是 removeAnnotations,它接受一个 MKAnnotations 数组,注意一个末尾的“s”,而不是另一个。 Attempting to cast from [MKAnnotation] , an array to MKAnnotation a single object or visa versa will crash the program.尝试从[MKAnnotation]将数组转换为MKAnnotation单个对象,反之亦然会使程序崩溃。 The line of code self.mapView.annotations creates an array. self.mapView.annotations 这行代码创建了一个数组。 Thus, if you are using the method removeAnnotation, you need to index the array for a single object within the array, as seen below:因此,如果您使用 removeAnnotation 方法,则需要为数组中的单个对象索引数组,如下所示:

let previousAnnotations = self.mapView.annotations
if !previousAnnotations.isEmpty{
  self.mapView.removeAnnotation(previousAnnotations[0])
}

Thus, you can remove various annotations while keeping the users location.因此,您可以在保留用户位置的同时删除各种注释。 You should always test your array before attempting to remove objects from it, else there is the possibly getting an out of bounds or nil error.在尝试从中删除对象之前,您应该始终测试您的数组,否则可能会出现越界或 nil 错误。

Note: using the method removeAnnotations (with an s)removes all annotations.注意:使用 removeAnnotations 方法(带有 s)删除所有注释。 If you are getting a nil, that implies that you have an empty array.如果你得到一个 nil,这意味着你有一个空数组。 You can verify that by adding an else statement after the if, like so;您可以通过在 if 之后添加 else 语句来验证这一点,如下所示;

    else{print("empty array")}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM