To install Cluster with CocoaPods, add this to your Podfile:
pod "Cluster"
Carthage
To install Cluster with Carthage, add this to your Cartfile:
github "efremidze/Cluster"
Usage
The Basics
The ClusterManager class generates, manages and displays annotation clusters.
let clusterManager = ClusterManager()
Adding an Annotation
Create an object that conforms to the MKAnnotation protocol, or extend an existing one. Next, add the annotation object to an instance of ClusterManager with add(annotation:).
let annotation = Annotation(coordinate: CLLocationCoordinate2D(latitude: 21.283921, longitude: -157.831661))
manager.add(annotation)
Configuring the Annotation View
Implement the map view’s mapView(_:viewFor:) delegate method to configure the annotation view. Return an instance of MKAnnotationView to display as a visual representation of the annotations.
To display clusters, return an instance of ClusterAnnotationView.
For performance reasons, you should generally reuse MKAnnotationView objects in your map views. See the Example to learn more.
Customizing the Appearance
The ClusterAnnotationView class exposes a countLabel property. You can subclass ClusterAnnotationView to provide custom behavior as needed. Here’s an example of subclassing the ClusterAnnotationView and customizing the layer borderColor.
To remove annotations, you can call remove(annotation:). However the annotations will still display until you call reload().
manager.remove(annotation)
In the case that shouldRemoveInvisibleAnnotations is set to false, annotations that have been removed may still appear on map until calling reload() on visible region.
Reloading Annotations
Implement the map view’s mapView(_:regionDidChangeAnimated:) delegate method to reload the ClusterManager when the region changes.
You should call reload() anytime you add or remove annotations.
Configuring the Manager
The ClusterManager class exposes several properties to configure clustering:
var zoomLevel: Double // The current zoom level of the visible map region.
var maxZoomLevel: Double // The maximum zoom level before disabling clustering.
var minCountForClustering: Int // The minimum number of annotations for a cluster. The default is `2`.
var shouldRemoveInvisibleAnnotations: Bool // Whether to remove invisible annotations. The default is `true`.
var shouldDistributeAnnotationsOnSameCoordinate: Bool // Whether to arrange annotations in a circle if they have the same coordinate. The default is `true`.
var distanceFromContestedLocation: Double // The distance in meters from contested location when the annotations have the same coordinate. The default is `3`.
var clusterPosition: ClusterPosition // The position of the cluster annotation. The default is `.nearCenter`.
ClusterManagerDelegate
The ClusterManagerDelegate protocol provides a number of functions to manage clustering and configure cells.
// The size of each cell on the grid at a given zoom level.
func cellSize(for zoomLevel: Double) -> Double? { ... }
// Whether to cluster the given annotation.
func shouldClusterAnnotation(_ annotation: MKAnnotation) -> Bool { ... }
Cluster is an easy map annotation clustering library. This repository uses an efficient method (QuadTree) to aggregate pins into a cluster.
Features
Requirements
Demo
The Example is a great place to get started. It demonstrates how to:
Demo Video
Installation
Cluster is available via CocoaPods and Carthage.
CocoaPods
To install Cluster with CocoaPods, add this to your
Podfile
:Carthage
To install Cluster with Carthage, add this to your
Cartfile
:Usage
The Basics
The
ClusterManager
class generates, manages and displays annotation clusters.Adding an Annotation
Create an object that conforms to the
MKAnnotation
protocol, or extend an existing one. Next, add the annotation object to an instance ofClusterManager
withadd(annotation:)
.Configuring the Annotation View
Implement the map view’s
mapView(_:viewFor:)
delegate method to configure the annotation view. Return an instance ofMKAnnotationView
to display as a visual representation of the annotations.To display clusters, return an instance of
ClusterAnnotationView
.For performance reasons, you should generally reuse
MKAnnotationView
objects in your map views. See the Example to learn more.Customizing the Appearance
The
ClusterAnnotationView
class exposes acountLabel
property. You can subclassClusterAnnotationView
to provide custom behavior as needed. Here’s an example of subclassing theClusterAnnotationView
and customizing the layerborderColor
.See the AnnotationView to learn more.
Annotation Styling
You can customize the appearance of the
StyledClusterAnnotationView
by setting thestyle
property of the annotation.Several styles are available in the
ClusterAnnotationStyle
enum:color(UIColor, radius: CGFloat)
- Displays the annotations as a circle.image(UIImage?)
- Displays the annotation as an image.Once you have added the annotation, you need to return an instance of the
StyledClusterAnnotationView
to display the styled annotation.Removing Annotations
To remove annotations, you can call
remove(annotation:)
. However the annotations will still display until you callreload()
.In the case that
shouldRemoveInvisibleAnnotations
is set tofalse
, annotations that have been removed may still appear on map until callingreload()
on visible region.Reloading Annotations
Implement the map view’s
mapView(_:regionDidChangeAnimated:)
delegate method to reload theClusterManager
when the region changes.You should call
reload()
anytime you add or remove annotations.Configuring the Manager
The
ClusterManager
class exposes several properties to configure clustering:ClusterManagerDelegate
The
ClusterManagerDelegate
protocol provides a number of functions to manage clustering and configure cells.Communication
Mentions
Credits
License
Cluster is available under the MIT license. See the LICENSE file for more info.