Easy Map Annotation Clustering 📍
Clone or download
Latest commit 3e5ba35 Dec 5, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Update CONTRIBUTING.md Dec 6, 2017
Cluster.xcodeproj Swift 4.2 (#96) Oct 19, 2018
Example 2.4.1 (#100) Dec 6, 2018
Images 1.0.3 Apr 30, 2017
Sources 2.4.1 (#100) Dec 6, 2018
Tests 2.4.1 (#100) Dec 6, 2018
docs Updated Documentation Nov 21, 2018
.gitignore Initial commit Apr 13, 2017
.swift-version Swift 4.2 (#96) Oct 19, 2018
.travis.yml Swift 4.2 (#96) Oct 19, 2018
CHANGELOG.md 2.4.1 (#100) Dec 6, 2018
Cluster.podspec 2.4.1 (#100) Dec 6, 2018
LICENSE Initial commit Apr 13, 2017
Package.swift Created Project Apr 13, 2017
README.md Update README.md Nov 8, 2018

README.md

Cluster

Build Status Language Version License Platform Carthage compatible


Cluster is an easy map annotation clustering library. This repository uses an efficient method (QuadTree) to aggregate pins into a cluster.

If you want to try it, simply run:

$ pod try Cluster

Features

  • Adding/Removing Annotations
  • Clustering Annotations
  • Multiple Managers
  • Dynamic Cluster Disabling
  • Custom Cell Size
  • Custom Annotation Views
  • Animation Support
  • Documentation

Requirements

  • iOS 8.0+
  • Xcode 9.0+
  • Swift 4 (Cluster 2.x), Swift 3 (Cluster 1.x)

Usage

Follow the instructions below:

Step 1: Initialize a ClusterManager object

let clusterManager = ClusterManager()

Step 2: Add annotations

let annotation = Annotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
annotation.style = .color(color, radius: 25) // .image(UIImage(named: "pin"))
clusterManager.add(annotation)

Step 3: Return the pins and clusters

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    var view = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
    if view == nil {
        view = ClusterAnnotationView(annotation: annotation, reuseIdentifier: identifier, style: style)
    } else {
        view?.annotation = annotation
    }
    return view
}

Step 4: Reload the annotations

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    clusterManager.reload(mapView) { finished in
        // handle completion
    }
}

ClusterManagerDelegate

The ClusterManagerDelegate protocol manages displaying annotations.

// 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 { ... }

Customization

The ClusterManager exposes several properties to customize 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 clusterPosition: ClusterPosition // The position of the cluster annotation. The default is `.nearCenter`.

Annotations

The Annotation class exposes a style property that allows you to customize the appearance.

var style: ClusterAnnotationStyle // The style of the cluster annotation view.

You can further customize the annotations by subclassing ClusterAnnotationView and overriding configure:

override func configure() {
    super.configure()

    // customize
}

Removing Annotations

To remove annotations, you can call remove(_ annotation: MKAnnotation). However the annotations will still display until you call reload().

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.

Installation

CocoaPods

To install with CocoaPods, simply add this in your Podfile:

use_frameworks!
pod "Cluster"

Carthage

To install with Carthage, simply add this in your Cartfile:

github "efremidze/Cluster"

Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Mentions

Credits

License

Cluster is available under the MIT license. See the LICENSE file for more info.