Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions REMarkerClusterer/REMarkerClusterer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
@optional

- (void)markerClusterer:(REMarkerClusterer *)markerCluster withMapView:(MKMapView *)mapView updateViewOfAnnotation:(id<MKAnnotation>)annotation withView:(MKAnnotationView *)annotationView;
- (void)willClusterize:(REMarkerClusterer *)markerClusterer;
- (void)didClusterize:(REMarkerClusterer *)markerClusterer;

@end

Expand Down
33 changes: 32 additions & 1 deletion REMarkerClusterer/REMarkerClusterer.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#include <libkern/OSAtomic.h>
#import "REMarkerClusterer.h"
#import "RECluster.h"

Expand Down Expand Up @@ -244,11 +245,19 @@ - (float)randomFloatBetween:(float)smallNumber and:(float)bigNumber
return (((float) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * diff) + smallNumber;
}

- (void)clusterizeFinished
{
if ([_delegate respondsToSelector:@selector(didClusterize:)])
[_delegate didClusterize:self];
}

- (void)splitAnnotationsWithDictionary:(NSDictionary *)dictionary
{
NSDictionary *mergeators = [dictionary objectForKey:mergeatorsKey];
NSDictionary *mixes = [dictionary objectForKey:mixesKey];

__block int32_t pendingAnimationsCount = 0;
BOOL didUseAnimation = NO;
for (NSString *mergeatorKey in [mergeators allKeys]){
NSArray *annotations = [mixes objectForKey:mergeatorKey];
RECluster *endCluster = [mergeators objectForKey:mergeatorKey];
Expand All @@ -260,6 +269,8 @@ - (void)splitAnnotationsWithDictionary:(NSDictionary *)dictionary
CLLocationCoordinate2D realCoordinate = annotation.coordinate;
annotation.coordinate = endCluster.coordinate;
_animating = YES;
didUseAnimation = YES;
OSAtomicIncrement32Barrier(&pendingAnimationsCount);
__typeof (&*self) __weak weakSelf = self;
[UIView animateWithDuration:[self randomFloatBetween:0.25 and:_maxDurationOfSplitAnimation] delay:[self randomFloatBetween:0 and:_maxDelayOfSplitAnimation]
options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
Expand All @@ -269,6 +280,8 @@ - (void)splitAnnotationsWithDictionary:(NSDictionary *)dictionary
weakSelf.animating = NO;
[_mapView removeAnnotation:annotation];
[_mapView addAnnotation:annotation];
if (OSAtomicDecrement32Barrier(&pendingAnimationsCount) <= 0)
[self clusterizeFinished];
}];
}
[_mapView removeAnnotation:endCluster];
Expand All @@ -279,18 +292,25 @@ - (void)splitAnnotationsWithDictionary:(NSDictionary *)dictionary
[_mapView removeAnnotation:endCluster];
}
}

if (!didUseAnimation)
[self clusterizeFinished];
}

- (void)joinAnnotationsWithDictionary:(NSDictionary *)dictionary
{
NSDictionary *mergeators = [dictionary objectForKey:mergeatorsKey];
NSDictionary *mixes = [dictionary objectForKey:mixesKey];

__block int32_t pendingAnimationsCount = 0;
BOOL didUseAnimation = NO;
for (NSString *mergeatorKey in [mergeators allKeys]) {
NSArray *annotations = [mixes objectForKey:mergeatorKey];
RECluster *endCluster = [mergeators objectForKey:mergeatorKey];
for (RECluster *annotation in annotations){
if (_animated) {
didUseAnimation = YES;
OSAtomicIncrement32Barrier(&pendingAnimationsCount);
_animating = YES;
__typeof (&*self) __weak weakSelf = self;
[UIView animateWithDuration:[self randomFloatBetween:0.25 and:_maxDurationOfSplitAnimation] delay:[self randomFloatBetween:0 and:_maxDelayOfSplitAnimation]
Expand All @@ -305,6 +325,8 @@ - (void)joinAnnotationsWithDictionary:(NSDictionary *)dictionary
[_mapView removeAnnotation:annotation];
[_mapView addAnnotation:annotation];
}
if (OSAtomicDecrement32Barrier(&pendingAnimationsCount) <= 0)
[self clusterizeFinished];
}];
}else{
[_mapView removeAnnotations:annotations];
Expand All @@ -320,12 +342,18 @@ - (void)joinAnnotationsWithDictionary:(NSDictionary *)dictionary
}
}
}

if (!didUseAnimation)
[self clusterizeFinished];
}

- (void)clusterize:(BOOL)animated
{
if (_animating && animated)
return;

if ([_delegate respondsToSelector:@selector(willClusterize:)])
[_delegate willClusterize:self];

_animated = animated;

Expand Down Expand Up @@ -381,13 +409,15 @@ - (void)clusterize:(BOOL)animated

if (self.markerAnnotations.count == 0) {
[_mapView addAnnotations:_clusters];
[self clusterizeFinished];
}
else if (self.markerAnnotations.count > _clusters.count) {
[self joinAnnotationsWithDictionary:dic];
//[self addAnnotationsWithOutSpliting:remainingAnnotations];
} else if(self.markerAnnotations.count < _clusters.count) {
[self splitAnnotationsWithDictionary:dic];
} else {
[self clusterizeFinished];
}

}
Expand Down Expand Up @@ -442,7 +472,8 @@ - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
[self clusterize:YES];
[self.mapView deselectAnnotation:[self.mapView.selectedAnnotations objectAtIndex:0] animated:NO];
if (![_delegate respondsToSelector:@selector(willClusterize:)])
[self.mapView deselectAnnotation:[self.mapView.selectedAnnotations objectAtIndex:0] animated:NO];

if ([_delegate respondsToSelector:@selector(mapView:regionDidChangeAnimated:)])
[_delegate mapView:mapView regionDidChangeAnimated:animated];
Expand Down