-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapAnnotationView.m
More file actions
153 lines (109 loc) · 4.11 KB
/
MapAnnotationView.m
File metadata and controls
153 lines (109 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// MapAnnotationView.m
// Sleep Blaster touch
//
// Created by Eamon Ford on 4/10/10.
// Copyright 2010 The Byte Factory. All rights reserved.
//
#import "MapAnnotationView.h"
#import "MapLinesAnnotation.h"
// this is an internally used view to CSRouteView. The CSRouteView needs a subview that does not get clipped to always
// be positioned at the full frame size and origin of the map. This way the view can be smaller than the route, but it
// always draws in the internal subview, which is the size of the map view.
@interface MapAnnotationViewInternal : UIView
{
// route view which added this as a subview.
MapAnnotationView* _annotationView;
}
@property (nonatomic, retain) MapAnnotationView *annotationView;
@end
@implementation MapAnnotationViewInternal
@synthesize annotationView = _annotationView;
-(id) init
{
self = [super init];
self.backgroundColor = [UIColor clearColor];
self.clipsToBounds = NO;
return self;
}
-(void) drawRect:(CGRect) rect
{
// only draw our lines if we're not in the middle of a transition and we
// acutally have some points to draw.
MapLinesAnnotation *annotation = (MapLinesAnnotation *)self.annotationView.annotation;
if(!self.hidden && annotation.points && annotation.points.count > 0)
{
CGContextRef context = UIGraphicsGetCurrentContext();
// if(nil == routeAnnotation.lineColor)
// routeAnnotation.lineColor = [UIColor blueColor]; // setting the property instead of the member variable will automatically reatin it.
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.6, 0.0, 0.2, 1.0);
CGFloat lengths[1] = {4.0};
CGContextSetLineDash(UIGraphicsGetCurrentContext(), 0, lengths, 1);
CGContextSetLineJoin(UIGraphicsGetCurrentContext(), kCGLineJoinRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 4.0);
for(int i = 0; i < [annotation.points count]; i++)
{
CLLocation* location = [annotation.points objectAtIndex:i];
CGPoint point = [self.annotationView.mapView convertCoordinate:location.coordinate toPointToView:self];
// point.y += 40;
if(i == 0)
{
// move to the first point
CGContextMoveToPoint(context, point.x, point.y);
} else {
CGContextAddLineToPoint(context, point.x, point.y);
}
}
CGContextStrokePath(context);
}
}
-(void) dealloc
{
self.annotationView = nil;
[super dealloc];
}
@end
@implementation MapAnnotationView
@synthesize mapView;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor clearColor];
// do not clip the bounds. We need the MapAnnotationViewInternal to be able to render the route, regardless of where the
// actual annotation view is displayed.
self.clipsToBounds = NO;
// create the internal annotation view that does the rendering of the route.
_internalAnnotationView = [[MapAnnotationViewInternal alloc] init];
_internalAnnotationView.annotationView = self;
[self addSubview:_internalAnnotationView];
}
return self;
}
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
self.clipsToBounds = NO;
// CGRect rect = CGRectMake(annotation.coordinate.longitude-(annotation.span.longitude/2), CGFloat y, CGFloat width, CGFloat height)
self.frame = CGRectMake(0, 0, 320, 480);
// create the internal annotation view that does the rendering of the route.
_internalAnnotationView = [[MapAnnotationViewInternal alloc] init];
_internalAnnotationView.annotationView = self;
[self addSubview:_internalAnnotationView];
return self;
}
- (void)regionChanged
{
// move the internal route view.
CGPoint origin = CGPointMake(0, 0);
origin = [mapView convertPoint:origin toView:self];
_internalAnnotationView.frame = CGRectMake(origin.x, origin.y, mapView.frame.size.width, mapView.frame.size.height);
[_internalAnnotationView setNeedsDisplay];
}
-(void) setMapView:(MKMapView*)newMapView
{
[mapView release];
mapView = [newMapView retain];
[self regionChanged];
}
@end