ribl / FBAnnotationClusteringSwift

Swift translation of FB Annotation Clustering, which clusters pins on the map for iOS.
http://ribl.co/blog/2015/05/28/map-clustering-with-swift-how-we-implemented-it-into-the-ribl-ios-app/
MIT License
311 stars 109 forks source link

Possibility for max cluster zoom level #26

Closed AF-cgi closed 8 years ago

AF-cgi commented 8 years ago

Thanks for the Swift translation. Is there any possibility to set a max zoom level for the clustering? If I achieve zoom level 14, I would like to see all annotations without clusters.

chenr2 commented 8 years ago

Would something like this work for you?

Change line 95-113 of FBViewController.swift from this:

let count = annotations.count

if count == 1 {
    clusteredAnnotations += annotations
}

if count > 1 {
    let coordinate = CLLocationCoordinate2D(
        latitude: CLLocationDegrees(totalLatitude)/CLLocationDegrees(count),
        longitude: CLLocationDegrees(totalLongitude)/CLLocationDegrees(count)
    )
    let cluster = FBAnnotationCluster()
    cluster.coordinate = coordinate
    cluster.annotations = annotations

    print("cluster.annotations.count:: \(cluster.annotations.count)")

    clusteredAnnotations.append(cluster)
}

to this:

let count = annotations.count

if count == 1 || cellSize == 16 {
    // Scenario 1: Single pin.
    // Scenario 2: The map is zoomed in all the way (cell size is 16)
    clusteredAnnotations += annotations
} else if count > 1 {
    let coordinate = CLLocationCoordinate2D(
        latitude: CLLocationDegrees(totalLatitude)/CLLocationDegrees(count),
        longitude: CLLocationDegrees(totalLongitude)/CLLocationDegrees(count)
    )
    let cluster = FBAnnotationCluster()
    cluster.coordinate = coordinate
    cluster.annotations = annotations

    clusteredAnnotations.append(cluster)
}               
jsdu commented 7 years ago

cellSize doesn't seem to work for me, instead I checked the zoomScale

Alex293 commented 7 years ago

How did you use the zoomScale can you provide a snippet ?

Alex293 commented 7 years ago

Without any modifications on code I use : extension MapController : FBClusteringManagerDelegate { public func cellSizeFactor(forCoordinator coordinator: FBClusteringManager) -> CGFloat { return 5.0 // Change this according to your needs } }

jsdu commented 7 years ago

Here's how I did it. I did change the code in FBClusteringManager.

if (zoomScale > 0.04) { clusteredAnnotations += annotations } else { switch count { case 0: break case 1: clusteredAnnotations += annotations default: let coordinate = CLLocationCoordinate2D( latitude: CLLocationDegrees(totalLatitude)/CLLocationDegrees(count), longitude: CLLocationDegrees(totalLongitude)/CLLocationDegrees(count) ) let cluster = FBAnnotationCluster() cluster.coordinate = coordinate cluster.annotations = annotations clusteredAnnotations.append(cluster) } }