Skip to content

Commit

Permalink
Fix for issue hulab#43.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafał Dubiel committed Dec 17, 2018
1 parent 66146c7 commit bd39a0e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
3 changes: 3 additions & 0 deletions ClusterKit/Core/CKCluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ MK_EXTERN NSComparisonResult MKMapSizeCompare(MKMapSize size1, MKMapSize size2);

#pragma mark - Cluster definitions


/**
CKCluster protocol that create a kind of CKCluster at the given coordinate.
*/
Expand Down Expand Up @@ -94,6 +95,8 @@ MK_EXTERN NSComparisonResult MKMapSizeCompare(MKMapSize size1, MKMapSize size2);
*/
@property (nonatomic, readonly) MKMapRect bounds;

- (NSMutableOrderedSet<id<MKAnnotation>> *) orderedSet;

/**
Adds a given annotation to the cluster, if it is not already a member.
Expand Down
4 changes: 4 additions & 0 deletions ClusterKit/Core/CKCluster.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ - (instancetype)init{
return self;
}

- (NSMutableOrderedSet<id<MKAnnotation>> *) orderedSet {
return _annotations;
}

- (NSArray<id<MKAnnotation>> *)annotations {
return _annotations.array;
}
Expand Down
40 changes: 24 additions & 16 deletions ClusterKit/Core/CKClusterManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,32 +180,40 @@ - (void)updateMapRect:(MKMapRect)visibleMapRect animated:(BOOL)animated {
CKClusterAlgorithm *algorithm = (zoom < self.maxZoomLevel)? self.algorithm : [CKClusterAlgorithm new];
NSArray *clusters = [algorithm clustersInRect:clusterMapRect zoom:zoom tree:self.tree];

//------HERE the moment that delete all duplicates from clusters
for (CKCluster* cluster in clusters) {
for (CKCluster* secondCluster in clusters) {

if (![secondCluster isEqualToCluster:cluster] && [secondCluster intersectsCluster:cluster]) {

NSMutableSet<id<MKAnnotation>> *secondSet = secondCluster.orderedSet.set.mutableCopy;

[cluster.orderedSet minusSet:secondSet];
}
}
}
//---------------------------------------
NSMutableSet *newClusters = [NSMutableSet setWithArray:clusters];
NSMutableSet *oldClusters = [NSMutableSet setWithSet:_clusters];

[oldClusters minusSet:newClusters];
[newClusters minusSet:_clusters];

NSComparisonResult zoomOrder = MKMapSizeCompare(_visibleMapRect.size, visibleMapRect.size);
_visibleMapRect = visibleMapRect;

switch (zoomOrder) {
case NSOrderedAscending:
[self collapse:oldClusters.allObjects to:newClusters.allObjects in:visibleMapRect];
break;

case NSOrderedDescending:
[self expand:newClusters.allObjects from:oldClusters.allObjects in:visibleMapRect];
break;

default:
[self.map addClusters:newClusters.allObjects];
[self.map removeClusters:oldClusters.allObjects];
break;
if (visibleMapRect.size.width > _visibleMapRect.size.width) {
[self collapse:oldClusters.allObjects to:newClusters.allObjects in:visibleMapRect];

} else if (visibleMapRect.size.width < _visibleMapRect.size.width) {
[self expand:newClusters.allObjects from:oldClusters.allObjects in:visibleMapRect];

} else {
[self.map addClusters:newClusters.allObjects];
[self.map removeClusters:oldClusters.allObjects];
}

[_clusters minusSet:oldClusters];
[_clusters unionSet:newClusters];

_visibleMapRect = visibleMapRect;
}

- (void)setSelectedCluster:(CKCluster *)selectedCluster animated:(BOOL)animated {
Expand Down

0 comments on commit bd39a0e

Please sign in to comment.