PageRenderTime 39ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/MapView/Map/RMMarkerManager.m

http://github.com/route-me/route-me
Objective C | 205 lines | 130 code | 34 blank | 41 comment | 19 complexity | eb7c2aeebd887e8a1b67cfeac0a08829 MD5 | raw file
  1. //
  2. // RMMarkerManager.m
  3. //
  4. // Copyright (c) 2008-2009, Route-Me Contributors
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are met:
  9. //
  10. // * Redistributions of source code must retain the above copyright notice, this
  11. // list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above copyright notice,
  13. // this list of conditions and the following disclaimer in the documentation
  14. // and/or other materials provided with the distribution.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. // POSSIBILITY OF SUCH DAMAGE.
  27. #import "RMMarkerManager.h"
  28. #import "RMMercatorToScreenProjection.h"
  29. #import "RMProjection.h"
  30. #import "RMLayerCollection.h"
  31. @implementation RMMarkerManager
  32. @synthesize contents;
  33. - (id)initWithContents:(RMMapContents *)mapContents
  34. {
  35. if (![super init])
  36. return nil;
  37. contents = mapContents;
  38. rotationTransform = CGAffineTransformIdentity;
  39. return self;
  40. }
  41. - (void)dealloc
  42. {
  43. contents = nil;
  44. [super dealloc];
  45. }
  46. ///////////////////////////////////////////////////////////////////////////////////////////////////
  47. #pragma mark
  48. #pragma mark Adding / Removing / Displaying Markers
  49. /// place the (new created) marker onto the map at projected point and take ownership of it
  50. - (void)addMarker:(RMMarker *)marker atProjectedPoint:(RMProjectedPoint)projectedPoint {
  51. // only set the AffineTransform if the marker has rotation enabled
  52. if (marker.enableRotation) {
  53. [marker setAffineTransform:rotationTransform];
  54. } else {
  55. [marker setAffineTransform:CGAffineTransformMakeRotation(0.0f)];
  56. }
  57. [marker setProjectedLocation:projectedPoint];
  58. [marker setPosition:[[contents mercatorToScreenProjection] projectXYPoint:projectedPoint]];
  59. [[contents overlay] addSublayer:marker];
  60. }
  61. /// place the (newly created) marker onto the map and take ownership of it
  62. /// \bug should return the marker
  63. - (void) addMarker: (RMMarker*)marker AtLatLong:(CLLocationCoordinate2D)point
  64. {
  65. [self addMarker:marker atProjectedPoint:[[contents projection] latLongToPoint:point]];
  66. }
  67. /// \bug see http://code.google.com/p/route-me/issues/detail?id=75
  68. /// (halmueller): I am skeptical about interactions of this code with paths
  69. - (void) removeMarkers
  70. {
  71. [[contents overlay] setSublayers:[NSArray arrayWithObjects:nil]];
  72. }
  73. /// \bug this will hide path overlays too?
  74. /// \deprecated syntactic sugar. Might have a place on RMMapView, but not on RMMarkerManager.
  75. - (void) hideAllMarkers
  76. {
  77. [[contents overlay] setHidden:YES];
  78. }
  79. /// \bug this will hide path overlays too?
  80. /// \deprecated syntactic sugar. Might have a place on RMMapView, but not on RMMarkerManager.
  81. - (void) unhideAllMarkers
  82. {
  83. [[contents overlay] setHidden:NO];
  84. }
  85. ///////////////////////////////////////////////////////////////////////////////////////////////////
  86. #pragma mark
  87. #pragma mark Marker information
  88. - (NSArray *)markers
  89. {
  90. return [[contents overlay] sublayers];
  91. }
  92. - (void) removeMarker:(RMMarker *)marker
  93. {
  94. [[contents overlay] removeSublayer:marker];
  95. }
  96. - (void) removeMarkers:(NSArray *)markers
  97. {
  98. [[contents overlay] removeSublayers:markers];
  99. }
  100. - (CGPoint) screenCoordinatesForMarker: (RMMarker *)marker
  101. {
  102. return [[contents mercatorToScreenProjection] projectXYPoint:[marker projectedLocation]];
  103. }
  104. - (CLLocationCoordinate2D) latitudeLongitudeForMarker: (RMMarker *) marker
  105. {
  106. return [contents pixelToLatLong:[self screenCoordinatesForMarker:marker]];
  107. }
  108. - (NSArray *) markersWithinScreenBounds
  109. {
  110. NSMutableArray *markersInScreenBounds = [NSMutableArray array];
  111. CGRect rect = [[contents mercatorToScreenProjection] screenBounds];
  112. for (RMMarker *marker in [self markers]) {
  113. if ([self isMarker:marker withinBounds:rect]) {
  114. [markersInScreenBounds addObject:marker];
  115. }
  116. }
  117. return markersInScreenBounds;
  118. }
  119. - (BOOL) isMarkerWithinScreenBounds:(RMMarker*)marker
  120. {
  121. return [self isMarker:marker withinBounds:[[contents mercatorToScreenProjection] screenBounds]];
  122. }
  123. /// \deprecated violates Objective-C naming rules
  124. - (BOOL) isMarker:(RMMarker*)marker withinBounds:(CGRect)rect
  125. {
  126. if (![self managingMarker:marker]) {
  127. return NO;
  128. }
  129. CGPoint markerCoord = [self screenCoordinatesForMarker:marker];
  130. if ( markerCoord.x > rect.origin.x
  131. && markerCoord.x < rect.origin.x + rect.size.width
  132. && markerCoord.y > rect.origin.y
  133. && markerCoord.y < rect.origin.y + rect.size.height)
  134. {
  135. return YES;
  136. }
  137. return NO;
  138. }
  139. /// \deprecated violates Objective-C naming rules
  140. - (BOOL) managingMarker:(RMMarker*)marker
  141. {
  142. NSArray *markers = [self markers];
  143. if (marker != nil && markers != nil && [markers indexOfObject:marker] != NSNotFound) {
  144. return YES;
  145. }
  146. return NO;
  147. }
  148. - (void) moveMarker:(RMMarker *)marker AtLatLon:(RMLatLong)point
  149. {
  150. [marker setProjectedLocation:[[contents projection]latLongToPoint:point]];
  151. [marker setPosition:[[contents mercatorToScreenProjection] projectXYPoint:[[contents projection] latLongToPoint:point]]];
  152. }
  153. - (void) moveMarker:(RMMarker *)marker AtXY:(CGPoint)point
  154. {
  155. [marker setProjectedLocation:[[contents mercatorToScreenProjection] projectScreenPointToXY:point]];
  156. [marker setPosition:point];
  157. }
  158. - (void)setRotation:(float)angle
  159. {
  160. rotationTransform = CGAffineTransformMakeRotation(angle); // store rotation transform for subsequent markers
  161. for (RMMarker *marker in [self markers])
  162. {
  163. // only apply rotation if the marker has it enabled
  164. if (marker.enableRotation) {
  165. [marker setAffineTransform:rotationTransform];
  166. } else {
  167. [marker setAffineTransform:CGAffineTransformMakeRotation(0.0f)];
  168. }
  169. }
  170. }
  171. @end