choefele/CCHMapClusterController

Using own custom MKAnnotation subclass for uniqueLocations

nnhubbard opened this issue · 4 comments

In our app, we have our own custom annotations that need to be used. So, when I detect a uniqueLocation I want to use our own MKAnnotationView subclass. Here is what I have so far:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKAnnotationView *annotationView;

    if ([annotation isKindOfClass:CCHMapClusterAnnotation.class]) {
        static NSString *identifier = @"clusterAnnotation";

        ZSSClusterAnnotationView *clusterAnnotationView = (ZSSClusterAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (clusterAnnotationView) {
            clusterAnnotationView.annotation = annotation;
        } else {
            clusterAnnotationView = [[ZSSClusterAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            clusterAnnotationView.canShowCallout = YES;
        }

        CCHMapClusterAnnotation *clusterAnnotation = (CCHMapClusterAnnotation *)annotation;
        clusterAnnotationView.count = clusterAnnotation.annotations.count;
        clusterAnnotationView.uniqueLocation = clusterAnnotation.isUniqueLocation;
        annotationView = clusterAnnotationView;

        // Check to see if this is a single annotation
        if (clusterAnnotation.isUniqueLocation) {
            if (clusterAnnotation.annotations.count == 1) {

                ZSSGeocache *geocacheAnnotation = clusterAnnotation.annotations.allObjects[0];
                ZSPinAnnotation *pinView = (ZSPinAnnotation *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"StandardCache"];
                if (pinView == nil){
                    pinView = [[ZSPinAnnotation alloc] initWithAnnotation:geocacheAnnotation reuseIdentifier:@"StandardCache"];
                }

                return pinView;

            }

        }

    }

    return annotationView;
}

This generally seems to work, however when I zoom out, I don't always get the pin clustering annotations. If I zoom back in I get them to show up.

How can I do what I am wanting here without really screwing up how CCHMapClusterController works?

Hi – you also have to handle mapClusterController:willReuseMapClusterAnnotation:. This will require a custom annotation view that can handle both cases (uniqueLocation is YES or NO).

The example in this project implements this here

What would I need to do in that method? Since it just returns void how do I control which annotation is reused?

The annotation view is created and initialized in mapView:(MKMapView *)mapView viewForAnnotation:, then updated in mapClusterController:willReuseMapClusterAnnotation: when reused. That's why the custom annotation view has to support both cases (displayed as a unique location or not)

Haven't heard back in a while – I assume this issue is solved