2
0

CCHMapClusterControllerUtils.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // CCHMapClusterControllerUtils.m
  3. // CCHMapClusterController
  4. //
  5. // Copyright (C) 2013 Claus Höfele
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #import "CCHMapClusterControllerUtils.h"
  26. #import "CCHMapClusterAnnotation.h"
  27. #import <float.h>
  28. #define fequal(a, b) (fabs((a) - (b)) < __FLT_EPSILON__)
  29. #define GEOHASH_LENGTH 9
  30. MKMapRect CCHMapClusterControllerAlignMapRectToCellSize(MKMapRect mapRect, double cellSize)
  31. {
  32. if (cellSize == 0) {
  33. return MKMapRectNull;
  34. }
  35. double startX = floor(MKMapRectGetMinX(mapRect) / cellSize) * cellSize;
  36. double startY = floor(MKMapRectGetMinY(mapRect) / cellSize) * cellSize;
  37. double endX = ceil(MKMapRectGetMaxX(mapRect) / cellSize) * cellSize;
  38. double endY = ceil(MKMapRectGetMaxY(mapRect) / cellSize) * cellSize;
  39. return MKMapRectMake(startX, startY, endX - startX, endY - startY);
  40. }
  41. CCHMapClusterAnnotation *CCHMapClusterControllerFindVisibleAnnotation(NSSet *annotations, NSSet *visibleAnnotations)
  42. {
  43. for (id<MKAnnotation> annotation in annotations) {
  44. for (CCHMapClusterAnnotation *visibleAnnotation in visibleAnnotations) {
  45. if ([visibleAnnotation.annotations containsObject:annotation]) {
  46. return visibleAnnotation;
  47. }
  48. }
  49. }
  50. return nil;
  51. }
  52. #if TARGET_OS_IPHONE
  53. double CCHMapClusterControllerMapLengthForLength(MKMapView *mapView, UIView *view, double length)
  54. #else
  55. double CCHMapClusterControllerMapLengthForLength(MKMapView *mapView, NSView *view, double length)
  56. #endif
  57. {
  58. // Convert points to coordinates
  59. CLLocationCoordinate2D leftCoordinate = [mapView convertPoint:CGPointZero toCoordinateFromView:view];
  60. CLLocationCoordinate2D rightCoordinate = [mapView convertPoint:CGPointMake(length, 0) toCoordinateFromView:view];
  61. // Convert coordinates to map points
  62. MKMapPoint leftMapPoint = MKMapPointForCoordinate(leftCoordinate);
  63. MKMapPoint rightMapPoint = MKMapPointForCoordinate(rightCoordinate);
  64. // Calculate distance between map points
  65. double xd = leftMapPoint.x - rightMapPoint.x;
  66. double yd = leftMapPoint.y - rightMapPoint.y;
  67. double mapLength = sqrt(xd*xd + yd*yd);
  68. // For very large lengths, we assume that we measured the other way around the world
  69. if (mapLength > (MKMapSizeWorld.width * 0.5)) {
  70. mapLength = MKMapSizeWorld.width - mapLength;
  71. }
  72. return mapLength;
  73. }
  74. double CCHMapClusterControllerAlignMapLengthToWorldWidth(double mapLength)
  75. {
  76. if (mapLength == 0) {
  77. return 0;
  78. }
  79. mapLength = MKMapSizeWorld.width / floor(MKMapSizeWorld.width / mapLength);
  80. return mapLength;
  81. }
  82. BOOL CCHMapClusterControllerCoordinateEqualToCoordinate(CLLocationCoordinate2D coordinate0, CLLocationCoordinate2D coordinate1)
  83. {
  84. BOOL isCoordinateUpToDate = fequal(coordinate0.latitude, coordinate1.latitude) && fequal(coordinate0.longitude, coordinate1.longitude);
  85. return isCoordinateUpToDate;
  86. }
  87. CCHMapClusterAnnotation *CCHMapClusterControllerClusterAnnotationForAnnotation(MKMapView *mapView, id<MKAnnotation> annotation, MKMapRect mapRect)
  88. {
  89. CCHMapClusterAnnotation *annotationResult;
  90. NSSet *mapAnnotations = [mapView annotationsInMapRect:mapRect];
  91. for (id<MKAnnotation> mapAnnotation in mapAnnotations) {
  92. if ([mapAnnotation isKindOfClass:CCHMapClusterAnnotation.class]) {
  93. CCHMapClusterAnnotation *mapClusterAnnotation = (CCHMapClusterAnnotation *)mapAnnotation;
  94. if (mapClusterAnnotation.annotations) {
  95. if ([mapClusterAnnotation.annotations containsObject:annotation]) {
  96. annotationResult = mapClusterAnnotation;
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. return annotationResult;
  103. }
  104. void CCHMapClusterControllerEnumerateCells(MKMapRect mapRect, double cellSize, void (^block)(MKMapRect cellMapRect))
  105. {
  106. NSCAssert(block != NULL, @"Block argument can't be NULL");
  107. if (block == nil) {
  108. return;
  109. }
  110. MKMapRect cellRect = MKMapRectMake(0, MKMapRectGetMinY(mapRect), cellSize, cellSize);
  111. while (MKMapRectGetMinY(cellRect) < MKMapRectGetMaxY(mapRect)) {
  112. cellRect.origin.x = MKMapRectGetMinX(mapRect);
  113. while (MKMapRectGetMinX(cellRect) < MKMapRectGetMaxX(mapRect)) {
  114. // Wrap around the origin's longitude
  115. MKMapRect rect = MKMapRectMake(fmod(cellRect.origin.x, MKMapSizeWorld.width), cellRect.origin.y, cellRect.size.width, cellRect.size.height);
  116. block(rect);
  117. cellRect.origin.x += MKMapRectGetWidth(cellRect);
  118. }
  119. cellRect.origin.y += MKMapRectGetWidth(cellRect);
  120. }
  121. }
  122. MKMapRect CCHMapClusterControllerMapRectForCoordinateRegion(MKCoordinateRegion coordinateRegion)
  123. {
  124. CLLocationCoordinate2D topLeftCoordinate = CLLocationCoordinate2DMake(coordinateRegion.center.latitude + (coordinateRegion.span.latitudeDelta / 2.0), coordinateRegion.center.longitude - (coordinateRegion.span.longitudeDelta / 2.0));
  125. MKMapPoint topLeftMapPoint = MKMapPointForCoordinate(topLeftCoordinate);
  126. CLLocationCoordinate2D bottomRightCoordinate = CLLocationCoordinate2DMake(coordinateRegion.center.latitude - (coordinateRegion.span.latitudeDelta / 2.0), coordinateRegion.center.longitude + (coordinateRegion.span.longitudeDelta / 2.0));
  127. MKMapPoint bottomRightMapPoint = MKMapPointForCoordinate(bottomRightCoordinate);
  128. MKMapRect mapRect = MKMapRectMake(topLeftMapPoint.x, topLeftMapPoint.y, fabs(bottomRightMapPoint.x - topLeftMapPoint.x), fabs(bottomRightMapPoint.y - topLeftMapPoint.y));
  129. return mapRect;
  130. }
  131. NSSet *CCHMapClusterControllerClusterAnnotationsForAnnotations(NSArray *annotations, CCHMapClusterController *mapClusterController)
  132. {
  133. NSSet *filteredAnnotations = [NSMutableSet setWithArray:annotations];
  134. filteredAnnotations = [filteredAnnotations filteredSetUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
  135. BOOL evaluation = NO;
  136. if ([evaluatedObject isKindOfClass:CCHMapClusterAnnotation.class]) {
  137. CCHMapClusterAnnotation *clusterAnnotation = (CCHMapClusterAnnotation *)evaluatedObject;
  138. evaluation = (clusterAnnotation.mapClusterController == mapClusterController);
  139. }
  140. return evaluation;
  141. }]];
  142. return filteredAnnotations;
  143. }
  144. NS_INLINE double originXForLongitudeAtZoomLevel22(CLLocationDegrees longitude)
  145. {
  146. const double MERCATOR_OFFSET = 536870912; // (width in points at zoom level 22) / 2
  147. const double MERCATOR_RADIUS_SCALE = MERCATOR_OFFSET / 180.0;
  148. return MERCATOR_OFFSET + MERCATOR_RADIUS_SCALE * longitude;
  149. }
  150. double CCHMapClusterControllerZoomLevelForRegion(CLLocationDegrees longitudeCenter, CLLocationDegrees longitudeDelta, CGFloat width)
  151. {
  152. // Based on http://troybrant.net/blog/2010/01/mkmapview-and-zoom-levels-a-visual-guide/
  153. // Adjusted so that at zoom level 0, the entire world fits into a single 256 point tile.
  154. const double LOG_2 = 0.69314718055994529; // log(2)
  155. double centerPointX = originXForLongitudeAtZoomLevel22(longitudeCenter);
  156. double topLeftPointX = originXForLongitudeAtZoomLevel22(longitudeCenter - longitudeDelta / 2);
  157. double scaledMapWidth = (centerPointX - topLeftPointX) * 2;
  158. double zoomScale = scaledMapWidth / width;
  159. double zoomExponent = log(zoomScale) / LOG_2;
  160. double zoomLevel = 22 - zoomExponent;
  161. return zoomLevel;
  162. }
  163. #define MAX_HASH_LENGTH 22
  164. #define SET_BIT(bits, mid, range, value, offset) \
  165. mid = ((range)->max + (range)->min) / 2.0; \
  166. if ((value) >= mid) { \
  167. (range)->min = mid; \
  168. (bits) |= (0x1 << (offset)); \
  169. } else { \
  170. (range)->max = mid; \
  171. (bits) |= (0x0 << (offset)); \
  172. }
  173. static const char BASE32_ENCODE_TABLE[33] = "0123456789bcdefghjkmnpqrstuvwxyz";
  174. typedef struct {
  175. double max;
  176. double min;
  177. } GEOHASH_range;
  178. static char *GEOHASH_encode(double lat, double lon, unsigned long len)
  179. {
  180. unsigned long i;
  181. char *hash;
  182. unsigned char bits = 0;
  183. double mid;
  184. GEOHASH_range lat_range = { 90, -90 };
  185. GEOHASH_range lon_range = { 180, -180 };
  186. double val1, val2, val_tmp;
  187. GEOHASH_range *range1, *range2, *range_tmp;
  188. assert(lat >= -90.0);
  189. assert(lat <= 90.0);
  190. assert(lon >= -180.0);
  191. assert(lon <= 180.0);
  192. assert(len <= MAX_HASH_LENGTH);
  193. hash = (char *)malloc(sizeof(char) * (len + 1));
  194. if (hash == NULL)
  195. return NULL;
  196. val1 = lon; range1 = &lon_range;
  197. val2 = lat; range2 = &lat_range;
  198. for (i=0; i < len; i++) {
  199. bits = 0;
  200. SET_BIT(bits, mid, range1, val1, 4);
  201. SET_BIT(bits, mid, range2, val2, 3);
  202. SET_BIT(bits, mid, range1, val1, 2);
  203. SET_BIT(bits, mid, range2, val2, 1);
  204. SET_BIT(bits, mid, range1, val1, 0);
  205. hash[i] = BASE32_ENCODE_TABLE[bits];
  206. val_tmp = val1;
  207. val1 = val2;
  208. val2 = val_tmp;
  209. range_tmp = range1;
  210. range1 = range2;
  211. range2 = range_tmp;
  212. }
  213. hash[len] = '\0';
  214. return hash;
  215. }
  216. static NSString *hashForCoordinate(CLLocationCoordinate2D coordinate, NSUInteger length)
  217. {
  218. // Based on https://github.com/lyokato/objc-geohash
  219. NSString *geohashAsString;
  220. char *geohash = GEOHASH_encode(coordinate.latitude, coordinate.longitude, length);
  221. if (geohash) {
  222. geohashAsString = [NSString stringWithCString:geohash encoding:NSASCIIStringEncoding];
  223. }
  224. free(geohash);
  225. return geohashAsString;
  226. }
  227. NSArray *CCHMapClusterControllerAnnotationSetsByUniqueLocations(NSSet *annotations, NSUInteger maxUniqueLocations)
  228. {
  229. NSMutableDictionary *annotationsByGeohash;
  230. if (maxUniqueLocations > 0) {
  231. annotationsByGeohash = [NSMutableDictionary dictionary];
  232. for (id<MKAnnotation> annotation in annotations) {
  233. // Add annotation to unique locations
  234. NSString *geohash = hashForCoordinate(annotation.coordinate, GEOHASH_LENGTH);
  235. NSMutableSet *annotationsAtLocation = [annotationsByGeohash objectForKey:geohash];
  236. if (!annotationsAtLocation) {
  237. annotationsAtLocation = [NSMutableSet set];
  238. }
  239. [annotationsAtLocation addObject:annotation];
  240. [annotationsByGeohash setObject:annotationsAtLocation forKey:geohash];
  241. // Return nil if max has been reached
  242. if (annotationsByGeohash.count > maxUniqueLocations) {
  243. annotationsByGeohash = nil;
  244. break;
  245. }
  246. }
  247. }
  248. return [annotationsByGeohash allValues];
  249. }
  250. BOOL CCHMapClusterControllerIsUniqueLocation(NSSet *annotations)
  251. {
  252. NSString *geohash;
  253. for (id<MKAnnotation> annotation in annotations) {
  254. NSString *updatedGeohash = hashForCoordinate(annotation.coordinate, GEOHASH_LENGTH);
  255. if (geohash == nil) {
  256. geohash = updatedGeohash;
  257. } else if (![geohash isEqualToString:updatedGeohash]) {
  258. geohash = nil;
  259. break;
  260. }
  261. }
  262. return (geohash != nil);
  263. }