- How to access cluster's annotations?
- How to disable clustering at a certain zoom level (multiple pins at same location)?
- How to configure annotations with custom images?
You can gain access to the cluster's annotations via -[KPAnnotation annotations]
.
The simplest solution that worked for everyone so far:
Find category for MKMapView which provides you with zoom level (one example).
Then implement clustering controller's delegate method:
- (BOOL)clusteringControllerShouldClusterAnnotations:(KPClusteringController *)clusteringController {
return self.mapView.zoomLevel < 14; // Find zoom level that suits your dataset
}
Note: Notice that in the following example it is MKAnnotationView
class that should be used as class or parent class for annotations with custom images, not the MKPinAnnotationView
! which is intended to work specifically with default annotation pin icons provided by Apple.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKAnnotationView *annotationView = nil;
if ([annotation isKindOfClass:[KPAnnotation class]]) {
KPAnnotation *kingpinAnnotation = (KPAnnotation *)annotation;
if ([kingpinAnnotation isCluster]) {
annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"cluster"];
if (annotationView == nil){
annotationView = [[MKAnnotationView alloc] initWithAnnotation:kingpinAnnotation reuseIdentifier:@"cluster"];
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:@"blue"];
}
}
else {
annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:[kingpinAnnotation.annotations anyObject]
reuseIdentifier:@"pin"];
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:@"green"];
}
}
}
return annotationView;
}