Skip to content

GoogleMaps

Maxime edited this page Oct 17, 2017 · 1 revision


GoogleMaps

Installation

CocoaPods

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

pod 'ClusterKit'

Then add the GMSMapView+ClusterKit file manually.

Carthage

With Carthage, add the following to your Cartfile:

github "hulab/ClusterKit"

Add GMSMapView+ClusterKit category

Note: These instructions are only for the case where you have a Podfile that declares use_frameworks! in it. If your Podfile doesn't have use_frameworks! in it, please use the 'ClusterKit/GoogleMaps' subspec directly:

pod 'ClusterKit/GoogleMaps'

As GoogleMaps is still a static framework, ClusterKit for GoogleMaps is not available as a framework dependency. if you aim to integrate ClusterKit with GoogleMaps, you will need few more steps:

  1. In your project, create a group named ClusterKit.
  2. Download GMSMapView+ClusterKit.h and GMSMapView+ClusterKit.m from ClusterKit/GoogleMaps.
  3. Add the files to your project by right clicking on the ClusterKit group and selecting "Add Files to ..." (Make sure you select the target as your app target to avoid undefined symbols issue).
  4. In Swift projects: Add a bridging header file with #import "GMSMapView+ClusterKit.h" (note the relative path).

Usage

Configure the cluster manager

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

Handle interactions in the map view's delegate

- (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position {
    [mapView.clusterManager updateClustersIfNeeded];
}

- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
    if (marker.cluster.count > 1) {
        GMSCameraUpdate *cameraUpdate = [GMSCameraUpdate fitCluster:marker.cluster];
        [mapView animateWithCameraUpdate:cameraUpdate];
        return YES;
    }
    return NO;
}

Provide cluster marker in datasource

- (GMSMarker *)mapView:(GMSMapView *)mapView markerForCluster:(CKCluster *)cluster {
    GMSMarker *marker = [GMSMarker markerWithPosition:cluster.coordinate];
    
    if(cluster.count > 1) {
        marker.icon = <#Cluster icon#>;
    } else {
        marker.icon = <#Annotation icon#>;
    }
    
    return marker;
}