Skip to content
Maxime edited this page Oct 17, 2017 · 1 revision


MapKit

Installation

CocoaPods

ClusterKit for MapKit is defined as a subspec in CocoaPods. To install it, simply add the following line to your Podfile:

pod 'ClusterKit/MapKit'

Import the framework

Objective-C:
#import <ClusterKit/MKMapView+ClusterKit.h>
Swift:
import ClusterKit

Carthage

ClusterKit for MapKit is defined as a submodule in its dynamic framework.

Import the framework

Objective-C:
#import <ClusterKit/MKMapView+ClusterKit.h>
Swift:
import ClusterKit.MapKit

Usage

Configure the cluster manager

CKNonHierarchicalDistanceBasedAlgorithm *algorithm = [CKNonHierarchicalDistanceBasedAlgorithm new];
self.mapView.clusterManager.algorithm = algorithm;
self.mapView.clusterManager.annotations = annotations;

Handle interactions in the map view's delegate

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    [mapView.clusterManager updateClustersIfNeeded];
}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    
    if ([view.annotation isKindOfClass:[CKCluster class]]) {
        CKCluster *cluster = view.annotation;
        
        if (cluster.count > 1) {
            [mapView showCluster:cluster animated:YES];
        }
    }
}