hulab / ClusterKit

An iOS map clustering framework targeting MapKit, Google Maps and Mapbox.
MIT License
515 stars 86 forks source link

Possible fix of the issue #43 #59

Closed oradyvan closed 6 years ago

oradyvan commented 6 years ago

https://github.com/hulab/ClusterKit/issues/43

Remove old clusters from the map before the animation of collapsing clusters is completed.

In my project, I was also experiencing a duplication of annotations in clusters, as I was picking up the new clusters in the method -mapView:regionDidChangeAnimated: like this:

    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        mapView.clusterManager.updateClustersIfNeeded()

        // determine if the clusters visible on the map have changed since the last time we checked
        let clusters = mapView.annotations(in: mapView.visibleMapRect).compactMap { $0 as? CKCluster }
        // do something with the clusters ...

It seems there was a subtle difference in 2 counterpart methods of CKClusterManager:

- (void)expand:(NSArray<CKCluster *> *)newClusters from:(NSArray<CKCluster *> *)oldClusters in:(MKMapRect)rect
// and
- (void)collapse:(NSArray<CKCluster *> *)oldClusters to:(NSArray<CKCluster *> *)newClusters in:(MKMapRect)rect

as one method was removing oldClusters BEFORE the animations were finished and the other method was removing those AFTER the animations were finished.

oradyvan commented 6 years ago

My apologies on publishing this PR too early - the problem seems to be of the totally different nature. My changes are breaking animations of collapsing multiple clusters into one, so I am closing this PR. I have another idea on how to access currently visible clusters, I will publish another PR as an improvement.