Closed AF-cgi closed 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)
}
cellSize doesn't seem to work for me, instead I checked the zoomScale
How did you use the zoomScale can you provide a snippet ?
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 } }
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) } }
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.