/
REVClusterManager.m
161 lines (127 loc) · 5.39 KB
/
REVClusterManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
//
// ___ _____ ______ __ _ _________
// / _ \/ __/ | / / __ \/ /| | / / __/ _ \
// / , _/ _/ | |/ / /_/ / /_| |/ / _// , _/
// /_/|_/___/ |___/\____/____/___/___/_/|_|
//
// Created by Bart Claessens. bart (at) revolver . be
//
#import "REVClusterManager.h"
#define BASE_RADIUS .5 // = 1 mile
#define MINIMUM_LATITUDE_DELTA 0.20
#define BLOCKS 4
#define MINIMUM_CLUSTER_LEVEL 100000
@implementation REVClusterManager
+ (NSArray *) clusterAnnotationsForMapView:(MKMapView *)mapView forAnnotations:(NSArray *)pins blocks:(NSUInteger)blocks minClusterLevel:(NSUInteger)minClusterLevel
{
NSMutableArray *visibleAnnotations = [NSMutableArray array];
double tileX = mapView.visibleMapRect.origin.x;
double tileY = mapView.visibleMapRect.origin.y;
float tileWidth = mapView.visibleMapRect.size.width/BLOCKS;
float tileHeight = mapView.visibleMapRect.size.height/BLOCKS;
MKMapRect mapRect = MKMapRectWorld;
NSUInteger maxWidthBlocks = round(mapRect.size.width / tileWidth);
float zoomLevel = maxWidthBlocks / BLOCKS;
float tileStartX = floorf(tileX/tileWidth)*tileWidth;
float tileStartY = floorf(tileY/tileHeight)*tileHeight;
MKMapRect visibleMapRect = MKMapRectMake(tileStartX, tileStartY, tileWidth*(BLOCKS+1), tileHeight*(BLOCKS+1));
for (id<MKAnnotation> point in pins)
{
MKMapPoint mapPoint = MKMapPointForCoordinate(point.coordinate);
if( MKMapRectContainsPoint(visibleMapRect,mapPoint) )
{
if( ![mapView.annotations containsObject:point] )
{
[visibleAnnotations addObject:point];
}
}
}
if( zoomLevel > minClusterLevel )
{
return visibleAnnotations;
}
NSMutableArray *clusteredBlocks = [NSMutableArray array];
int i = 0;
int length = (BLOCKS+1)*(BLOCKS+1);
for ( ; i < length ; i ++ )
{
REVClusterBlock *block = [[REVClusterBlock alloc] init];
[clusteredBlocks addObject:block];
#if !__has_feature(objc_arc)
[block release];
#endif
}
for (REVClusterPin *pin in visibleAnnotations)
{
MKMapPoint mapPoint = MKMapPointForCoordinate(pin.coordinate);
double localPointX = mapPoint.x - tileStartX;
double localPointY = mapPoint.y - tileStartY;
int localTileNumberX = floor( localPointX / tileWidth );
int localTileNumberY = floor( localPointY / tileHeight );
int localTileNumber = localTileNumberX + (localTileNumberY * (BLOCKS+1));
[(REVClusterBlock *)[clusteredBlocks objectAtIndex:localTileNumber] addAnnotation:pin];
}
//create New Pins
NSMutableArray *newPins = [NSMutableArray array];
for ( REVClusterBlock *block in clusteredBlocks )
{
if( [block count] > 0 )
{
if( ![REVClusterManager clusterAlreadyExistsForMapView:mapView andBlockCluster:block] )
{
[newPins addObject:[block getClusteredAnnotation]];
}
}
}
return newPins;
}
+ (BOOL) clusterAlreadyExistsForMapView:(MKMapView *)mapView andBlockCluster:(REVClusterBlock *)cluster
{
for ( REVClusterPin *pin in mapView.annotations )
{
if( [pin isKindOfClass:[REVClusterPin class]] && [[pin nodes] count] > 0 )
{
MKMapPoint point1 = MKMapPointForCoordinate([pin coordinate]);
MKMapPoint point2 = MKMapPointForCoordinate([[cluster getClusteredAnnotation] coordinate]);
if( MKMapPointEqualToPoint(point1,point2) )
{
return YES;
}
}
}
return NO;
}
+ (NSArray *) clusterForMapView:(MKMapView *)mapView forAnnotations:(NSArray *)pins
{
return [self clusterAnnotationsForMapView:mapView forAnnotations:pins blocks:BLOCKS minClusterLevel:MINIMUM_CLUSTER_LEVEL];
}
+ (MKPolygon *)polygonForMapRect:(MKMapRect)mapRect
{
MKMapPoint mapRectPoints[5]={
MKMapPointMake(mapRect.origin.x, mapRect.origin.y),
MKMapPointMake(mapRect.origin.x+mapRect.size.width, mapRect.origin.y),
MKMapPointMake(mapRect.origin.x+mapRect.size.width, mapRect.origin.y+mapRect.size.height),
MKMapPointMake(mapRect.origin.x, mapRect.origin.y+mapRect.size.height),
MKMapPointMake(mapRect.origin.x, mapRect.origin.y)
};
return [MKPolygon polygonWithPoints:mapRectPoints count:5];
}
- (NSInteger)getGlobalTileNumberFromMapView:(MKMapView *)mapView forLocalTileNumber:(NSInteger)tileNumber
{
double tileX = mapView.visibleMapRect.origin.x;
double tileY = mapView.visibleMapRect.origin.y;
double tileWidth = mapView.visibleMapRect.size.width/BLOCKS;
double tileHeight = mapView.visibleMapRect.size.height/BLOCKS;
MKMapRect mapRect = MKMapRectWorld;
NSUInteger maxWidthBlocks = round(mapRect.size.width / tileWidth);
NSUInteger maxHeightBlocks = round(mapRect.size.height / tileHeight);
double tileStartX = floor((tileX/mapRect.size.width) * maxWidthBlocks)*tileWidth;
double tileStartY = floor((tileY/mapRect.size.height) * maxHeightBlocks)*tileHeight;
double currentTileX = tileStartX + (tileWidth * (tileNumber % (BLOCKS+1)));
double currentTileY = tileStartY + (tileHeight * floor(tileNumber/(BLOCKS+1)));
NSInteger g = round((currentTileY / tileHeight) * maxWidthBlocks);
g += round(currentTileX / tileWidth);
return g;
}
@end