2
0

CCHMapTree.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // CCHMapTree.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 "CCHMapTree.h"
  26. #import "CCHMapTreeUtils.h"
  27. @interface CCHMapTree()
  28. @property (nonatomic) NSMutableSet *mutableAnnotations;
  29. @property (nonatomic) CCHMapTreeNode *root;
  30. @property (nonatomic) NSUInteger nodeCapacity;
  31. @end
  32. @implementation CCHMapTree
  33. - (instancetype)init
  34. {
  35. return [self initWithNodeCapacity:10 minLatitude:-85.0 maxLatitude:85.0 minLongitude:-180.0 maxLongitude:180.0];
  36. }
  37. - (instancetype)initWithNodeCapacity:(NSUInteger)nodeCapacity minLatitude:(double)minLatitude maxLatitude:(double)maxLatitude minLongitude:(double)minLongitude maxLongitude:(double)maxLongitude
  38. {
  39. self = [super init];
  40. if (self) {
  41. _nodeCapacity = nodeCapacity;
  42. _mutableAnnotations = [NSMutableSet set];
  43. CCHMapTreeBoundingBox world = CCHMapTreeBoundingBoxMake(minLatitude, minLongitude, maxLatitude, maxLongitude);
  44. _root = CCHMapTreeBuildWithData(NULL, 0, world, nodeCapacity);
  45. }
  46. return self;
  47. }
  48. - (void)dealloc
  49. {
  50. CCHMapTreeFreeQuadTreeNode(self.root);
  51. }
  52. - (NSSet *)annotations
  53. {
  54. return [self.mutableAnnotations copy];
  55. }
  56. - (BOOL)addAnnotations:(NSArray *)annotations
  57. {
  58. BOOL updated = NO;
  59. NSMutableSet *mutableAnnotations = self.mutableAnnotations;
  60. for (id<MKAnnotation> annotation in annotations) {
  61. if (![mutableAnnotations containsObject:annotation]) {
  62. CCHMapTreeNodeData data = CCHMapTreeNodeDataMake(annotation.coordinate.latitude, annotation.coordinate.longitude, (__bridge void *)annotation);
  63. if (CCHMapTreeNodeInsertData(_root, data, (int)_nodeCapacity)) {
  64. updated = YES;
  65. [mutableAnnotations addObject:annotation];
  66. }
  67. }
  68. }
  69. return updated;
  70. }
  71. - (BOOL)removeAnnotations:(NSArray *)annotations
  72. {
  73. BOOL updated = NO;
  74. NSMutableSet *mutableAnnotations = self.mutableAnnotations;
  75. for (id<MKAnnotation> annotation in annotations) {
  76. id<MKAnnotation> member = [mutableAnnotations member:annotation];
  77. if (member) {
  78. CCHMapTreeNodeData data = CCHMapTreeNodeDataMake(annotation.coordinate.latitude, annotation.coordinate.longitude, (__bridge void *)member);
  79. if (CCHMapTreeNodeRemoveData(_root, data)) {
  80. updated = YES;
  81. [mutableAnnotations removeObject:annotation];
  82. }
  83. }
  84. }
  85. return updated;
  86. }
  87. CCHMapTreeBoundingBox CCHMapTreeBoundingBoxForMapRect(MKMapRect mapRect)
  88. {
  89. CLLocationCoordinate2D topLeft = MKCoordinateForMapPoint(mapRect.origin);
  90. CLLocationCoordinate2D botRight = MKCoordinateForMapPoint(MKMapPointMake(MKMapRectGetMaxX(mapRect), MKMapRectGetMaxY(mapRect)));
  91. CLLocationDegrees minLat = botRight.latitude;
  92. CLLocationDegrees maxLat = topLeft.latitude;
  93. CLLocationDegrees minLon = topLeft.longitude;
  94. CLLocationDegrees maxLon = botRight.longitude;
  95. return CCHMapTreeBoundingBoxMake(minLat, minLon, maxLat, maxLon);
  96. }
  97. - (NSSet *)annotationsInMapRect:(MKMapRect)mapRect
  98. {
  99. CCHMapTreeUnsafeMutableArray *annotations = [[CCHMapTreeUnsafeMutableArray alloc] initWithCapacity:10];
  100. CCHMapTreeGatherDataInRange3(self.root, CCHMapTreeBoundingBoxForMapRect(mapRect), annotations);
  101. NSSet *annotationsAsSet = [NSSet setWithObjects:annotations.objects count:annotations.numObjects];
  102. return annotationsAsSet;
  103. }
  104. @end