/MapView/Map/RMMarker.m

http://github.com/route-me/route-me · Objective C · 214 lines · 152 code · 32 blank · 30 comment · 16 complexity · 540dd93ef11c1c8b5ec4af112843bfa4 MD5 · raw file

  1. //
  2. // RMMarker.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 "RMMarker.h"
  28. #import "RMPixel.h"
  29. @implementation RMMarker
  30. @synthesize projectedLocation;
  31. @synthesize enableDragging;
  32. @synthesize enableRotation;
  33. @synthesize data;
  34. @synthesize label;
  35. @synthesize textForegroundColor;
  36. @synthesize textBackgroundColor;
  37. #define defaultMarkerAnchorPoint CGPointMake(0.5, 0.5)
  38. + (UIFont *)defaultFont
  39. {
  40. return [UIFont systemFontOfSize:15];
  41. }
  42. // init
  43. - (id)init
  44. {
  45. if ((self = [super init])) {
  46. label = nil;
  47. textForegroundColor = [UIColor blackColor];
  48. textBackgroundColor = [UIColor clearColor];
  49. enableDragging = YES;
  50. enableRotation = YES;
  51. }
  52. return self;
  53. }
  54. - (id) initWithUIImage: (UIImage*) image
  55. {
  56. return [self initWithUIImage:image anchorPoint: defaultMarkerAnchorPoint];
  57. }
  58. - (id) initWithUIImage: (UIImage*) image anchorPoint: (CGPoint) _anchorPoint
  59. {
  60. if (![self init])
  61. return nil;
  62. self.contents = (id)[image CGImage];
  63. self.bounds = CGRectMake(0,0,image.size.width,image.size.height);
  64. self.anchorPoint = _anchorPoint;
  65. self.masksToBounds = NO;
  66. self.label = nil;
  67. return self;
  68. }
  69. - (void) replaceUIImage: (UIImage*) image
  70. {
  71. [self replaceUIImage:image anchorPoint:defaultMarkerAnchorPoint];
  72. }
  73. - (void) replaceUIImage: (UIImage*) image
  74. anchorPoint: (CGPoint) _anchorPoint
  75. {
  76. self.contents = (id)[image CGImage];
  77. self.bounds = CGRectMake(0,0,image.size.width,image.size.height);
  78. self.anchorPoint = _anchorPoint;
  79. self.masksToBounds = NO;
  80. }
  81. - (void) setLabel:(UIView*)aView
  82. {
  83. if (label == aView) {
  84. return;
  85. }
  86. if (label != nil)
  87. {
  88. [[label layer] removeFromSuperlayer];
  89. [label release];
  90. label = nil;
  91. }
  92. if (aView != nil)
  93. {
  94. label = [aView retain];
  95. [self addSublayer:[label layer]];
  96. }
  97. }
  98. - (void) changeLabelUsingText: (NSString*)text
  99. {
  100. CGPoint position = CGPointMake([self bounds].size.width / 2 - [text sizeWithFont:[RMMarker defaultFont]].width / 2, 4);
  101. /// \bug hardcoded font name
  102. [self changeLabelUsingText:text position:position font:[RMMarker defaultFont] foregroundColor:[self textForegroundColor] backgroundColor:[self textBackgroundColor]];
  103. }
  104. - (void) changeLabelUsingText: (NSString*)text position:(CGPoint)position
  105. {
  106. [self changeLabelUsingText:text position:position font:[RMMarker defaultFont] foregroundColor:[self textForegroundColor] backgroundColor:[self textBackgroundColor]];
  107. }
  108. - (void) changeLabelUsingText: (NSString*)text font:(UIFont*)font foregroundColor:(UIColor*)textColor backgroundColor:(UIColor*)backgroundColor
  109. {
  110. CGPoint position = CGPointMake([self bounds].size.width / 2 - [text sizeWithFont:font].width / 2, 4);
  111. [self setTextForegroundColor:textColor];
  112. [self setTextBackgroundColor:backgroundColor];
  113. [self changeLabelUsingText:text position:position font:font foregroundColor:textColor backgroundColor:backgroundColor];
  114. }
  115. - (void) changeLabelUsingText: (NSString*)text position:(CGPoint)position font:(UIFont*)font foregroundColor:(UIColor*)textColor backgroundColor:(UIColor*)backgroundColor
  116. {
  117. CGSize textSize = [text sizeWithFont:font];
  118. CGRect frame = CGRectMake(position.x,
  119. position.y,
  120. textSize.width+4,
  121. textSize.height+4);
  122. UILabel *aLabel = [[UILabel alloc] initWithFrame:frame];
  123. [self setTextForegroundColor:textColor];
  124. [self setTextBackgroundColor:backgroundColor];
  125. [aLabel setNumberOfLines:0];
  126. [aLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
  127. [aLabel setBackgroundColor:backgroundColor];
  128. [aLabel setTextColor:textColor];
  129. [aLabel setFont:font];
  130. [aLabel setTextAlignment:UITextAlignmentCenter];
  131. [aLabel setText:text];
  132. [self setLabel:aLabel];
  133. [aLabel release];
  134. }
  135. - (void) toggleLabel
  136. {
  137. if (self.label == nil) {
  138. return;
  139. }
  140. if ([self.label isHidden]) {
  141. [self showLabel];
  142. } else {
  143. [self hideLabel];
  144. }
  145. }
  146. - (void) showLabel
  147. {
  148. if ([self.label isHidden]) {
  149. // Using addSublayer will animate showing the label, whereas setHidden is not animated
  150. [self addSublayer:[self.label layer]];
  151. [self.label setHidden:NO];
  152. }
  153. }
  154. - (void) hideLabel
  155. {
  156. if (![self.label isHidden]) {
  157. // Using removeFromSuperlayer will animate hiding the label, whereas setHidden is not animated
  158. [[self.label layer] removeFromSuperlayer];
  159. [self.label setHidden:YES];
  160. }
  161. }
  162. - (void) dealloc
  163. {
  164. self.data = nil;
  165. self.label = nil;
  166. self.textForegroundColor = nil;
  167. self.textBackgroundColor = nil;
  168. [super dealloc];
  169. }
  170. - (void)zoomByFactor: (float) zoomFactor near:(CGPoint) center
  171. {
  172. if(enableDragging){
  173. self.position = RMScaleCGPointAboutPoint(self.position, zoomFactor, center);
  174. }
  175. }
  176. - (void)moveBy: (CGSize) delta
  177. {
  178. if(enableDragging){
  179. [super moveBy:delta];
  180. }
  181. }
  182. @end