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


Mapbox

Installation

CocoaPods

ClusterKit For Mapbox is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'ClusterKit/Mapbox'

Usage

Configure the cluster manager

Objective-C
CKGridBasedAlgorithm *algorithm = [CKGridBasedAlgorithm new];
self.mapView.clusterManager.algorithm = algorithm;
self.mapView.dataSource = self;
self.mapView.clusterManager.annotations = annotations;
Swift
let algorithm = CKNonHierarchicalDistanceBasedAlgorithm()
mapView.clusterManager.algorithm = algorithm
mapView.clusterManager.annotations = annotations

Handle interactions in the map view's delegate

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

- (void)mapView:(MGLMapView *)mapView didSelectAnnotation:(nonnull id<MGLAnnotation>)annotation {
    CKCluster *cluster = (CKCluster *)annotation;
    
    if (cluster.count > 1) {
        UIEdgeInsets edgePadding = UIEdgeInsetsMake(40, 20, 44, 20);
        MGLMapCamera *camera = [mapView cameraThatFitsCluster:cluster edgePadding:edgePadding];
        [mapView setCamera:camera animated:YES];
    }
}
Swift
func mapView(_ mapView: MGLMapView, regionDidChangeAnimated animated: Bool) {
    mapView.clusterManager.updateClustersIfNeeded()
}

func mapView(_ mapView: MGLMapView, didSelect annotation: MGLAnnotation) {

    if let cluster = annotation as? CKCluster, cluster.count > 1 {
        let edgePadding = UIEdgeInsetsMake(40, 20, 44, 20)
        let camera = mapView.cameraThatFitsCluster(cluster, edgePadding: edgePadding)
        mapView.setCamera(camera, animated: true)
    }
}