FBAnnotationClustering удалить аннотацию в swift

Я использую FBAnnotationClustering библиотека стручка какао в моем приложении, чтобы сделать кластеризацию. В этом removeAllAnnotation Метод доступен только в Objective-C, но не в Swift 3. Поэтому я публикую быструю версию, чтобы помочь другим, таким как я. Пожалуйста, смотрите мой собственный ответ ниже.

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

}
Другие вопросы по тегам