6

I’ve been trying out the new iOS 11 MKMarkerAnnotationView, and finally clustering works like a charm. But the problem is that the clusters have the wrong color as you can see in the picture. How can I control this? Wrong cluster color

Anders Frohm
  • 401
  • 5
  • 18

2 Answers2

12

Nevermind I managed to figure it out.

I added the following to the mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) function of the delegate.

if let cluster = annotation as? MKClusterAnnotation {
    let markerAnnotationView = MKMarkerAnnotationView()
    markerAnnotationView.glyphText = String(cluster.memberAnnotations.count)
    markerAnnotationView.markerTintColor = UIColor.appPrimary
    markerAnnotationView.canShowCallout = false

    return markerAnnotationView
}
edwardmp
  • 6,339
  • 5
  • 50
  • 77
Anders Frohm
  • 401
  • 5
  • 18
0

if you are not using MKMapViewDelegate you can just set, in your MKMarkerAnnotationView subclass,

clusteringIdentifier = MKMapViewDefaultClusterAnnotationViewReuseIdentifier

and in your view controller

mapView.register(SubclassMarkerView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

mapView.register(SubclassMarkerView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)
Tharak
  • 128
  • 4