/AnnotationClustering

Framework that clusters annotations on MKMapView.

Primary LanguageMakefileMIT LicenseMIT

AnnotationClustering

Platform iOS8+

Language: Swift 2 Carthage compatible

Framework that clusters annotations on MKMapView.

Based on https://github.com/ribl/FBAnnotationClusteringSwift.

Requirements

iOS 8.0+, Swift 2.3

Usage

An example app is included to demonstrate the usage of AnnotationClustering.

Getting started

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
            }
        }
    }
}

Documentation

You can find the API documentation here: Documentation

Installation

Carthage

Add the following line to your Cartfile.

github "gunterhager/AnnotationClustering"

Then run carthage update.

Manually

Just drag and drop the .swift files in the AnnotationClustering folder into your project.

License

AnnotationClustering is available under the MIT license. See the LICENSE file for details.

Contact

Made with ❤ at all about apps.

all about apps