FBAnnotationClustering在swift中删除注释

时间:2016-10-22 08:40:28

标签: ios swift mapkit mapkitannotation

我在我的应用程序中使用FBAnnotationClustering cocoa pod库来进行群集。在这个removeAllAnnotation方法只在Objective-C中可用,但在swift 3中不可用。所以我发布了swift版本来帮助像我这样的人。请在下面查看我自己的答案。

1 个答案:

答案 0 :(得分:1)

<强> FBClusteringManager.swift:

open func removeAnnotations() {
    if tree == nil {
        return
    }
    lock.lock()
    for annotation: MKAnnotation in allAnnotations() {
        tree!.remove(annotation)
    }
    lock.unlock()
}

<强> FBQuadTree.swift

func remove(_ annotation: MKAnnotation) -> Bool {
    return self.remove(annotation, from: rootNode!)
}

func remove(_ annotation: MKAnnotation, from node: FBQuadTreeNode) -> Bool {

    if !FBQuadTreeNode.FBBoundingBoxContainsCoordinate(node.boundingBox!, coordinate: annotation.coordinate) {
        return false
    }

    do {
        if let index = node.annotations.index(where: {self.equate(lhs: $0, rhs: annotation)}) {
            node.annotations.remove(at: index)
            node.count -= 1
        }
    } catch {
        return false
    }

    if let northEast = node.northEast {
        if self.remove(annotation, from: northEast) {
            return true
        }
    }

    if let northWest = node.northWest {
        if self.remove(annotation, from: northWest) {
            return true
        }
    }

    if let southEast = node.southEast {
        if self.remove(annotation, from: southEast) {
            return true
        }
    }

    if let southWest = node.southWest {
        if self.remove(annotation, from: southWest) {
            return true
        }
    }

    return false
}

func equate(lhs: MKAnnotation, rhs: MKAnnotation) -> Bool{
    return lhs.coordinate.latitude == rhs.coordinate.latitude
        && lhs.coordinate.longitude == rhs.coordinate.longitude

}