Wrong cluster size when zooming out
JustusvonBrandt opened this issue · 2 comments
I have implemented the clustering and it works fine, when I use the default MKAnnotationViews, so when I am not overriding the method mapView:ViewForAnnotation:
But I want to have different colored annotations for different sized clusters. I have implemented the method mapView:viewForAnnotation: as following:
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("AnnotationView")
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView")
}
annotationView?.image = UIImage(named: "OneLocationAnnotationImage")!
if annotation.isKindOfClass(CCHMapClusterAnnotation) {
let clusterAnnotation = annotation as! CCHMapClusterAnnotation
let count = clusterAnnotation.annotations.count
if count == 1 {
annotationView?.image = UIImage(named: "OneLocationAnnotationImage")!
} else if count < 3 {
annotationView?.image = UIImage(named: "LowDensityAnnotation")!
} else if count < 5 {
annotationView?.image = UIImage(named: "LowMediumDensityAnnotation")!
} else if count < 7 {
annotationView?.image = UIImage(named: "HighMediumDensityAnnotation")!
} else {
annotationView?.image = UIImage(named: "HighDensityAnnotation")!
}
return annotationView
}
return annotationView
When I now zoom in, the clustering works fine, but when I zoom out, the image is not returning to the one at the beginning, which should be the case. So the question is, if I am doing the right approach to get to the result I want?
The annotation views are recycled when zooming in and out. This means that the image needs to be updated in both mapView:viewForAnnotation:
as well as mapClusterController:willReuseMapClusterAnnotation:
. I suggest you have a look at MapViewController
that comes with the iOS example – this class implements both methods to update the image displayed for the annotation views.
Thank you, that was the hint I needed :) Now the clustered annotations work properly!
The CCHMapClusterController is really well done and easy to implement, but there are two things I would change, because then it is even easier to use:
I had to search for the ClusterAnnotationView class in the example project, maybe you could include them in the Framework as "CCHClusterAnnotationView.h/m" and you could create an header importing all .h files, so that when you are using Swift you only have to import one file in the bridging header.