【问题标题】:FBAnnotationClustering remove annotation in swiftFBAnnotationClustering 快速删除注释
【发布时间】:2016-10-22 08:40:28
【问题描述】:

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

【问题讨论】:

    标签: ios swift mapkit mapkitannotation


    【解决方案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
    
    }
    

    【讨论】:

    • 你能 .zip 整个库吗?
    猜你喜欢
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2016-12-10
    • 2011-07-23
    • 2016-07-25
    相关资源
    最近更新 更多