Authored by Obrand69

Added methods to hide and unhide marker at the marker level. Has not tested the …

…code so it may not compile. Fix as necessary...
1 -//  
2 -// RMMarker.h  
3 -// MapView  
4 -//  
5 -// Created by Joseph Gentle on 13/10/08.  
6 -// Copyright 2008 __MyCompanyName__. All rights reserved.  
7 -//  
8 -  
9 -#import <UIKit/UIKit.h>  
10 -#import "RMMapLayer.h"  
11 -#import "RMFoundation.h"  
12 -  
13 -@class RMMarkerStyle;  
14 -  
15 -extern NSString * const RMMarkerBlueKey;  
16 -extern NSString * const RMMarkerRedKey;  
17 -  
18 -@interface RMMarker : RMMapLayer <RMMovingMapLayer> {  
19 - RMXYPoint location;  
20 - NSObject* data;  
21 -  
22 - // A label which comes up when you tap the marker  
23 - UILabel* label;  
24 -}  
25 -  
26 -+ (RMMarker*) markerWithNamedStyle: (NSString*) styleName;  
27 -+ (CGImageRef) markerImage: (NSString *) key;  
28 -+ (CGImageRef) loadPNGFromBundle: (NSString *)filename;  
29 -  
30 -- (id) initWithCGImage: (CGImageRef) image anchorPoint: (CGPoint) anchorPoint;  
31 -- (id) initWithCGImage: (CGImageRef) image;  
32 -- (id) initWithKey: (NSString*) key;  
33 -- (id) initWithUIImage: (UIImage*) image;  
34 -- (id) initWithStyle: (RMMarkerStyle*) style;  
35 -- (id) initWithNamedStyle: (NSString*) styleName;  
36 -  
37 -- (void) setLabel: (UILabel*)aLabel;  
38 -- (void) setTextLabel: (NSString*)text;  
39 -- (void) setTextLabel: (NSString*)text toPosition:(CGPoint)position;  
40 -- (void) toggleLabel;  
41 -- (void) showLabel;  
42 -- (void) hideLabel;  
43 -- (void) removeLabel;  
44 -  
45 -- (void) replaceImage:(CGImageRef)image anchorPoint:(CGPoint)_anchorPoint;  
46 -  
47 -- (void) dealloc;  
48 -  
49 -@property (assign, nonatomic) RMXYPoint location;  
50 -@property (retain) NSObject* data;  
51 -@property (nonatomic, retain) UILabel* label;  
52 -  
53 -// Call this with either RMMarkerBlue or RMMarkerRed for the key.  
54 -+ (CGImageRef) markerImage: (NSString *) key;  
55 -  
56 -@end 1 +//
  2 +// RMMarker.h
  3 +// MapView
  4 +//
  5 +// Created by Joseph Gentle on 13/10/08.
  6 +// Copyright 2008 __MyCompanyName__. All rights reserved.
  7 +//
  8 +
  9 +#import <UIKit/UIKit.h>
  10 +#import "RMMapLayer.h"
  11 +#import "RMFoundation.h"
  12 +
  13 +@class RMMarkerStyle;
  14 +
  15 +extern NSString * const RMMarkerBlueKey;
  16 +extern NSString * const RMMarkerRedKey;
  17 +
  18 +@interface RMMarker : RMMapLayer <RMMovingMapLayer> {
  19 + RMXYPoint location;
  20 + NSObject* data;
  21 +
  22 + // A label which comes up when you tap the marker
  23 + UILabel* label;
  24 +}
  25 +
  26 ++ (RMMarker*) markerWithNamedStyle: (NSString*) styleName;
  27 ++ (CGImageRef) markerImage: (NSString *) key;
  28 ++ (CGImageRef) loadPNGFromBundle: (NSString *)filename;
  29 +
  30 +- (id) initWithCGImage: (CGImageRef) image anchorPoint: (CGPoint) anchorPoint;
  31 +- (id) initWithCGImage: (CGImageRef) image;
  32 +- (id) initWithKey: (NSString*) key;
  33 +- (id) initWithUIImage: (UIImage*) image;
  34 +- (id) initWithStyle: (RMMarkerStyle*) style;
  35 +- (id) initWithNamedStyle: (NSString*) styleName;
  36 +
  37 +- (void) setLabel: (UILabel*)aLabel;
  38 +- (void) setTextLabel: (NSString*)text;
  39 +- (void) setTextLabel: (NSString*)text toPosition:(CGPoint)position;
  40 +- (void) toggleLabel;
  41 +- (void) showLabel;
  42 +- (void) hideLabel;
  43 +- (void) removeLabel;
  44 +
  45 +- (void) replaceImage:(CGImageRef)image anchorPoint:(CGPoint)_anchorPoint;
  46 +- (void) hide;
  47 +- (void) unhide;
  48 +
  49 +- (void) dealloc;
  50 +
  51 +@property (assign, nonatomic) RMXYPoint location;
  52 +@property (retain) NSObject* data;
  53 +@property (nonatomic, retain) UILabel* label;
  54 +
  55 +// Call this with either RMMarkerBlue or RMMarkerRed for the key.
  56 ++ (CGImageRef) markerImage: (NSString *) key;
  57 +
  58 +@end
1 -//  
2 -// RMMarker.m  
3 -// MapView  
4 -//  
5 -// Created by Joseph Gentle on 13/10/08.  
6 -// Copyright 2008 __MyCompanyName__. All rights reserved.  
7 -//  
8 -  
9 -#import "RMMarker.h"  
10 -#import "RMMarkerStyle.h"  
11 -#import "RMMarkerStyles.h"  
12 -  
13 -#import "RMPixel.h"  
14 -  
15 -NSString* const RMMarkerBlueKey = @"RMMarkerBlueKey";  
16 -NSString* const RMMarkerRedKey = @"RMMarkerRedKey";  
17 -  
18 -static CGImageRef _markerRed = nil;  
19 -static CGImageRef _markerBlue = nil;  
20 -  
21 -@implementation RMMarker  
22 -  
23 -@synthesize location;  
24 -@synthesize data;  
25 -@synthesize label;  
26 -  
27 -+ (RMMarker*) markerWithNamedStyle: (NSString*) styleName  
28 -{  
29 - return [[[RMMarker alloc] initWithNamedStyle: styleName] autorelease];  
30 -}  
31 -  
32 -- (id) initWithCGImage: (CGImageRef) image  
33 -{  
34 - return [self initWithCGImage: image anchorPoint: CGPointMake(0.5, 1.0)];  
35 -}  
36 -  
37 -- (id) initWithCGImage: (CGImageRef) image anchorPoint: (CGPoint) _anchorPoint  
38 -{  
39 - if (![super init])  
40 - return nil;  
41 -  
42 - self.contents = (id)image;  
43 - self.bounds = CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image));  
44 - self.anchorPoint = _anchorPoint;  
45 -  
46 - self.masksToBounds = NO;  
47 - label = nil;  
48 -  
49 - return self;  
50 -}  
51 -  
52 -- (void) replaceImage:(CGImageRef)image anchorPoint:(CGPoint)_anchorPoint  
53 -{  
54 - self.contents = (id)image;  
55 - self.bounds = CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image));  
56 - self.anchorPoint = _anchorPoint;  
57 -  
58 - self.masksToBounds = NO;  
59 - label = nil;  
60 -}  
61 -  
62 -- (id) initWithUIImage: (UIImage*) image  
63 -{  
64 - return [self initWithCGImage: [image CGImage]];  
65 -}  
66 -  
67 -- (id) initWithKey: (NSString*) key  
68 -{  
69 - return [self initWithCGImage:[RMMarker markerImage:key]];  
70 -}  
71 -  
72 -- (id) initWithStyle: (RMMarkerStyle*) style  
73 -{  
74 - return [self initWithCGImage: [style.markerIcon CGImage] anchorPoint: style.anchorPoint];  
75 -}  
76 -  
77 -- (id) initWithNamedStyle: (NSString*) styleName  
78 -{  
79 - RMMarkerStyle* style = [[RMMarkerStyles styles] styleNamed: styleName];  
80 -  
81 - if (style==nil) {  
82 - NSLog(@"problem creating marker: style '%@' not found", styleName);  
83 - return [self initWithCGImage: [RMMarker markerImage: RMMarkerRedKey]];  
84 - }  
85 - return [self initWithStyle: style];  
86 -}  
87 -  
88 -- (void) setLabel: (UILabel*)aLabel  
89 -{  
90 - if (label != nil)  
91 - {  
92 - [[label layer] removeFromSuperlayer];  
93 - [label release];  
94 - label = nil;  
95 - }  
96 -  
97 - if (aLabel != nil)  
98 - {  
99 - label = [aLabel retain];  
100 - [self addSublayer:[label layer]];  
101 - }  
102 -}  
103 -  
104 -- (void) setTextLabel: (NSString*)text  
105 -{  
106 - [self setTextLabel:text toPosition:CGPointMake([self bounds].size.width / 2 - [text sizeWithFont:[UIFont systemFontOfSize:15]].width / 2, 4)];;  
107 -}  
108 -  
109 -- (void) setTextLabel: (NSString*)text toPosition:(CGPoint)position  
110 -{  
111 - CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:15]];  
112 - CGRect frame = CGRectMake(position.x,  
113 - position.y,  
114 - textSize.width+4,  
115 - textSize.height+4);  
116 -  
117 - frame.size = [text sizeWithFont:[UIFont systemFontOfSize:15]];  
118 -  
119 - UILabel *aLabel = [[UILabel alloc] initWithFrame:frame];  
120 - // UITextField *aLabel = [[UITextField alloc] initWithFrame:frame];  
121 - // [aLabel setBorderStyle:UITextBorderStyleRoundedRect];  
122 - // [aLabel setNumberOfLines:1];  
123 - [aLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];  
124 - [aLabel setBackgroundColor:[UIColor clearColor]];  
125 - [aLabel setTextColor:[UIColor blackColor]];  
126 - [aLabel setFont:[UIFont systemFontOfSize:15]];  
127 - [aLabel setTextAlignment:UITextAlignmentCenter];  
128 - [aLabel setText:text];  
129 - // [aLabel setCenter:CGPointMake(,0)];  
130 -  
131 - [self setLabel:aLabel];  
132 - [aLabel release];  
133 -  
134 -}  
135 -  
136 -- (void) removeLabel  
137 -{  
138 - if (label != nil)  
139 - {  
140 - [[label layer] removeFromSuperlayer];  
141 - [label release];  
142 - label = nil;  
143 - }  
144 -  
145 -}  
146 -  
147 -- (void) toggleLabel  
148 -{  
149 - if (label == nil) {  
150 - return;  
151 - }  
152 -  
153 - if ([label isHidden]) {  
154 - [self showLabel];  
155 - } else {  
156 - [self hideLabel];  
157 - }  
158 -}  
159 -  
160 -- (void) showLabel  
161 -{  
162 - if ([label isHidden]) {  
163 - // Using addSublayer will animate showing the label, whereas setHidden is not animated  
164 - [self addSublayer:[label layer]];  
165 - [label setHidden:NO];  
166 - }  
167 -}  
168 -  
169 -- (void) hideLabel  
170 -{  
171 - if (![label isHidden]) {  
172 - // Using removeFromSuperlayer will animate hiding the label, whereas setHidden is not animated  
173 - [[label layer] removeFromSuperlayer];  
174 - [label setHidden:YES];  
175 - }  
176 -}  
177 -  
178 -- (void) dealloc  
179 -{  
180 - [label release];  
181 - [data release];  
182 - [super dealloc];  
183 -}  
184 -  
185 -- (void)zoomByFactor: (float) zoomFactor near:(CGPoint) center  
186 -{  
187 - self.position = RMScaleCGPointAboutPoint(self.position, zoomFactor, center);  
188 -  
189 -/* CGRect currentRect = CGRectMake(self.position.x, self.position.y, self.bounds.size.width, self.bounds.size.height);  
190 - CGRect newRect = RMScaleCGRectAboutPoint(currentRect, zoomFactor, center);  
191 - self.position = newRect.origin;  
192 - self.bounds = CGRectMake(0, 0, newRect.size.width, newRect.size.height);  
193 -*/  
194 -}  
195 -  
196 -+ (CGImageRef) loadPNGFromBundle: (NSString *)filename  
197 -{  
198 - NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"png"];  
199 - CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename([path UTF8String]);  
200 - CGImageRef image = CGImageCreateWithPNGDataProvider(dataProvider, NULL, FALSE, kCGRenderingIntentDefault);  
201 - [NSMakeCollectable(image) autorelease];  
202 - CGDataProviderRelease(dataProvider);  
203 -  
204 - return image;  
205 -}  
206 -  
207 -+ (CGImageRef) markerImage: (NSString *) key  
208 -{  
209 - if (RMMarkerBlueKey == key  
210 - || [RMMarkerBlueKey isEqualToString:key])  
211 - {  
212 - if (_markerBlue == nil)  
213 - _markerBlue = [self loadPNGFromBundle:@"marker-blue"];  
214 -  
215 - return _markerBlue;  
216 - }  
217 - else if (RMMarkerRedKey == key  
218 - || [RMMarkerRedKey isEqualToString: key])  
219 - {  
220 - if (_markerRed == nil)  
221 - _markerRed = [self loadPNGFromBundle:@"marker-red"];  
222 -  
223 - return _markerRed;  
224 - }  
225 -  
226 - return nil;  
227 -}  
228 -  
229 -/*- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
230 -{  
231 -// [label setAlpha:1.0f - [label alpha]];  
232 -// [self setTextLabel:@"hello there"];  
233 - // NSLog(@"marker at %f %f m %f %f touchesEnded", self.position.x, self.position.y, location.x, location.y);  
234 -}*/  
235 -  
236 -@end 1 +//
  2 +// RMMarker.m
  3 +// MapView
  4 +//
  5 +// Created by Joseph Gentle on 13/10/08.
  6 +// Copyright 2008 __MyCompanyName__. All rights reserved.
  7 +//
  8 +
  9 +#import "RMMarker.h"
  10 +#import "RMMarkerStyle.h"
  11 +#import "RMMarkerStyles.h"
  12 +
  13 +#import "RMPixel.h"
  14 +
  15 +NSString* const RMMarkerBlueKey = @"RMMarkerBlueKey";
  16 +NSString* const RMMarkerRedKey = @"RMMarkerRedKey";
  17 +
  18 +static CGImageRef _markerRed = nil;
  19 +static CGImageRef _markerBlue = nil;
  20 +
  21 +@implementation RMMarker
  22 +
  23 +@synthesize location;
  24 +@synthesize data;
  25 +@synthesize label;
  26 +
  27 ++ (RMMarker*) markerWithNamedStyle: (NSString*) styleName
  28 +{
  29 + return [[[RMMarker alloc] initWithNamedStyle: styleName] autorelease];
  30 +}
  31 +
  32 +- (id) initWithCGImage: (CGImageRef) image
  33 +{
  34 + return [self initWithCGImage: image anchorPoint: CGPointMake(0.5, 1.0)];
  35 +}
  36 +
  37 +- (id) initWithCGImage: (CGImageRef) image anchorPoint: (CGPoint) _anchorPoint
  38 +{
  39 + if (![super init])
  40 + return nil;
  41 +
  42 + self.contents = (id)image;
  43 + self.bounds = CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image));
  44 + self.anchorPoint = _anchorPoint;
  45 +
  46 + self.masksToBounds = NO;
  47 + label = nil;
  48 +
  49 + return self;
  50 +}
  51 +
  52 +- (void) replaceImage:(CGImageRef)image anchorPoint:(CGPoint)_anchorPoint
  53 +{
  54 + self.contents = (id)image;
  55 + self.bounds = CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image));
  56 + self.anchorPoint = _anchorPoint;
  57 +
  58 + self.masksToBounds = NO;
  59 + label = nil;
  60 +}
  61 +
  62 +- (id) initWithUIImage: (UIImage*) image
  63 +{
  64 + return [self initWithCGImage: [image CGImage]];
  65 +}
  66 +
  67 +- (id) initWithKey: (NSString*) key
  68 +{
  69 + return [self initWithCGImage:[RMMarker markerImage:key]];
  70 +}
  71 +
  72 +- (id) initWithStyle: (RMMarkerStyle*) style
  73 +{
  74 + return [self initWithCGImage: [style.markerIcon CGImage] anchorPoint: style.anchorPoint];
  75 +}
  76 +
  77 +- (id) initWithNamedStyle: (NSString*) styleName
  78 +{
  79 + RMMarkerStyle* style = [[RMMarkerStyles styles] styleNamed: styleName];
  80 +
  81 + if (style==nil) {
  82 + NSLog(@"problem creating marker: style '%@' not found", styleName);
  83 + return [self initWithCGImage: [RMMarker markerImage: RMMarkerRedKey]];
  84 + }
  85 + return [self initWithStyle: style];
  86 +}
  87 +
  88 +- (void) setLabel: (UILabel*)aLabel
  89 +{
  90 + if (label != nil)
  91 + {
  92 + [[label layer] removeFromSuperlayer];
  93 + [label release];
  94 + label = nil;
  95 + }
  96 +
  97 + if (aLabel != nil)
  98 + {
  99 + label = [aLabel retain];
  100 + [self addSublayer:[label layer]];
  101 + }
  102 +}
  103 +
  104 +- (void) setTextLabel: (NSString*)text
  105 +{
  106 + [self setTextLabel:text toPosition:CGPointMake([self bounds].size.width / 2 - [text sizeWithFont:[UIFont systemFontOfSize:15]].width / 2, 4)];;
  107 +}
  108 +
  109 +- (void) setTextLabel: (NSString*)text toPosition:(CGPoint)position
  110 +{
  111 + CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:15]];
  112 + CGRect frame = CGRectMake(position.x,
  113 + position.y,
  114 + textSize.width+4,
  115 + textSize.height+4);
  116 +
  117 + frame.size = [text sizeWithFont:[UIFont systemFontOfSize:15]];
  118 +
  119 + UILabel *aLabel = [[UILabel alloc] initWithFrame:frame];
  120 + // UITextField *aLabel = [[UITextField alloc] initWithFrame:frame];
  121 + // [aLabel setBorderStyle:UITextBorderStyleRoundedRect];
  122 + // [aLabel setNumberOfLines:1];
  123 + [aLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
  124 + [aLabel setBackgroundColor:[UIColor clearColor]];
  125 + [aLabel setTextColor:[UIColor blackColor]];
  126 + [aLabel setFont:[UIFont systemFontOfSize:15]];
  127 + [aLabel setTextAlignment:UITextAlignmentCenter];
  128 + [aLabel setText:text];
  129 + // [aLabel setCenter:CGPointMake(,0)];
  130 +
  131 + [self setLabel:aLabel];
  132 + [aLabel release];
  133 +
  134 +}
  135 +
  136 +- (void) removeLabel
  137 +{
  138 + if (label != nil)
  139 + {
  140 + [[label layer] removeFromSuperlayer];
  141 + [label release];
  142 + label = nil;
  143 + }
  144 +
  145 +}
  146 +
  147 +- (void) toggleLabel
  148 +{
  149 + if (label == nil) {
  150 + return;
  151 + }
  152 +
  153 + if ([label isHidden]) {
  154 + [self showLabel];
  155 + } else {
  156 + [self hideLabel];
  157 + }
  158 +}
  159 +
  160 +- (void) showLabel
  161 +{
  162 + if ([label isHidden]) {
  163 + // Using addSublayer will animate showing the label, whereas setHidden is not animated
  164 + [self addSublayer:[label layer]];
  165 + [label setHidden:NO];
  166 + }
  167 +}
  168 +
  169 +- (void) hideLabel
  170 +{
  171 + if (![label isHidden]) {
  172 + // Using removeFromSuperlayer will animate hiding the label, whereas setHidden is not animated
  173 + [[label layer] removeFromSuperlayer];
  174 + [label setHidden:YES];
  175 + }
  176 +}
  177 +
  178 +- (void) dealloc
  179 +{
  180 + [label release];
  181 + [data release];
  182 + [super dealloc];
  183 +}
  184 +
  185 +- (void)zoomByFactor: (float) zoomFactor near:(CGPoint) center
  186 +{
  187 + self.position = RMScaleCGPointAboutPoint(self.position, zoomFactor, center);
  188 +
  189 +/* CGRect currentRect = CGRectMake(self.position.x, self.position.y, self.bounds.size.width, self.bounds.size.height);
  190 + CGRect newRect = RMScaleCGRectAboutPoint(currentRect, zoomFactor, center);
  191 + self.position = newRect.origin;
  192 + self.bounds = CGRectMake(0, 0, newRect.size.width, newRect.size.height);
  193 +*/
  194 +}
  195 +
  196 ++ (CGImageRef) loadPNGFromBundle: (NSString *)filename
  197 +{
  198 + NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"png"];
  199 + CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename([path UTF8String]);
  200 + CGImageRef image = CGImageCreateWithPNGDataProvider(dataProvider, NULL, FALSE, kCGRenderingIntentDefault);
  201 + [NSMakeCollectable(image) autorelease];
  202 + CGDataProviderRelease(dataProvider);
  203 +
  204 + return image;
  205 +}
  206 +
  207 ++ (CGImageRef) markerImage: (NSString *) key
  208 +{
  209 + if (RMMarkerBlueKey == key
  210 + || [RMMarkerBlueKey isEqualToString:key])
  211 + {
  212 + if (_markerBlue == nil)
  213 + _markerBlue = [self loadPNGFromBundle:@"marker-blue"];
  214 +
  215 + return _markerBlue;
  216 + }
  217 + else if (RMMarkerRedKey == key
  218 + || [RMMarkerRedKey isEqualToString: key])
  219 + {
  220 + if (_markerRed == nil)
  221 + _markerRed = [self loadPNGFromBundle:@"marker-red"];
  222 +
  223 + return _markerRed;
  224 + }
  225 +
  226 + return nil;
  227 +}
  228 +
  229 +- (void) hide
  230 +{
  231 + [self setHidden:YES];
  232 +}
  233 +
  234 +- (void) unhide
  235 +{
  236 + [self setHidden:NO];
  237 +}
  238 +
  239 +
  240 +/*- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  241 +{
  242 +// [label setAlpha:1.0f - [label alpha]];
  243 +// [self setTextLabel:@"hello there"];
  244 + // NSLog(@"marker at %f %f m %f %f touchesEnded", self.position.x, self.position.y, location.x, location.y);
  245 +}*/
  246 +
  247 +@end