Framework that clusters annotations on MKMapView
.
Based on https://github.com/ribl/FBAnnotationClusteringSwift.
iOS 8.0+, Swift 2.3
An example app is included to demonstrate the usage of AnnotationClustering
.
Create an instance of the cluster manager.
let clusterManager = ClusterManager()
Create annotations and add them to the cluster manager.
clusterManager.addAnnotations(annotations)
clusterManager.delegate = self
Return cluster or pin in the delegate of the map view.
extension ViewController : MKMapViewDelegate {
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool){
NSOperationQueue().addOperationWithBlock { [unowned self] in
let mapBoundsWidth = Double(mapView.bounds.size.width)
let mapRectWidth:Double = mapView.visibleMapRect.size.width
let scale:Double = mapBoundsWidth / mapRectWidth
let annotationArray = self.clusterManager.clusteredAnnotationsWithinMapRect(self.mapView.visibleMapRect, withZoomScale:scale)
self.clusterManager.displayAnnotations(annotationArray, mapView: mapView)
}
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
var reuseId = ""
if annotation.isKindOfClass(AnnotationCluster) {
reuseId = "Cluster"
if let clusterView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? AnnotationClusterView {
clusterView.reuseWithAnnotation(annotation)
return clusterView
}
else {
let clusterView = AnnotationClusterView(annotation: annotation, reuseIdentifier: reuseId, options: nil)
return clusterView
}
}
else {
reuseId = "Pin"
if let pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView {
pinView.annotation = annotation
return pinView
}
else {
let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
return pinView
}
}
}
}
You can find the API documentation here: Documentation
Add the following line to your Cartfile.
github "gunterhager/AnnotationClustering"
Then run carthage update
.
Just drag and drop the .swift
files in the AnnotationClustering
folder into your project.
AnnotationClustering
is available under the MIT license. See the LICENSE file for details.
Made with ❤ at all about apps.