4

I'm trying to add custom clusters to my map by using MKClusterAnnotation.To do that, I configure a subclass of MKAnnotationView and inside of it I configure the cluster itself. After that, I register my cluster in map view's view controller by this code (ClusterView is an MKAnnotationView subclass where I define my cluster):

 mapView.register(ClusterView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)

The problem is, I see the standard clusters on my map. None of the methods inside of the ClusterView class gets called (breakpoints don't fire inside of the class). The code for registering the cluster view doesn't get called, too. For that reason, I think there is something wrong with registering my subclass, but I don't see what. I've also tried to follow the code from WWDC 2017 "What's new in MapKit" session's example, but that didn't help. I don't use Map View's delegate method for clusters, because I want only one type of a cluster. However, I do use delegate methods for my annotation views, because they differ. Maybe, because of that I should also use delegate methods for clusters? If that is the case, how can I use them to add my custom clusters? There is almost nothing on the Internet about adding clusters to a map view by using MKClusterAnnotation, so I don't know what to do. Do you have any ideas what could be wrong?

Tigran Iskandaryan
  • 1,371
  • 2
  • 14
  • 41
  • 1
    You need to implement `clusterAnnotationForMemberAnnotations`: https://developer.apple.com/documentation/mapkit/mkmapviewdelegate/2867923-mapview?language=objc – Brandon Nov 02 '17 at 22:23
  • 1
    @Brandon, I've seen that method, but in the example code from the above mentioned session, cluster is added to map view by just registering it, but in my code that doesn't work. I would like to know the reason. – Tigran Iskandaryan Nov 02 '17 at 22:28
  • 1
    Make sure you're setting `clusteringIdentifier`. – chrisp Nov 06 '17 at 11:25
  • @chrisp, yeah that was the reason. And for adding custom clusters should I just register them or use MKMapViewDelegate methods? If you know how to do that, could you explain the process, please? – Tigran Iskandaryan Nov 06 '17 at 14:03
  • Source: https://developer.apple.com/videos/play/wwdc2017/237/ – chrisp Nov 06 '17 at 14:11
  • @chrisp, Yes. I've seen the video and the sample project's code from the session. I just can't figure out why my registration code doesn't get called in viewDidLoad(), because my map does show default clusters, but I would like to customise them a little bit. Anyway, thanks for the link. – Tigran Iskandaryan Nov 06 '17 at 14:14
  • 2
    Take care with the new register method on MKMapView as it is buggy, you should still use the MKMapViewDelegate methods: https://forums.developer.apple.com/thread/89427 – Eli Kohen Nov 27 '17 at 11:15
  • @EliKohen, thanks. It really helped :) – Tigran Iskandaryan Nov 30 '17 at 18:47

0 Answers0