-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathASPolylineRenderer.m
More file actions
123 lines (100 loc) · 2.82 KB
/
ASPolylineRenderer.m
File metadata and controls
123 lines (100 loc) · 2.82 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
//
// ASPolylineRenderer.m
//
// Created by Adrian Schoenig on 01/08/13.
//
//
#import "ASPolylineRenderer.h"
@interface ASPolylineRenderer () {
CGMutablePathRef _mutablePath;
}
@property (nonatomic, strong) MKPolyline *polyline;
@end
@implementation ASPolylineRenderer
- (id)initWithPolyline:(MKPolyline *)polyline
{
self = [super initWithOverlay:polyline];
if (self) {
self.polyline = polyline;
_mutablePath = CGPathCreateMutable();
[self constructPath];
// defaults
self.borderColor = [ASColor blackColor];
self.backgroundColor = [ASColor whiteColor];
self.borderMultiplier = 2.0;
}
return self;
}
- (void)dealloc
{
CGPathRelease(_mutablePath);
_mutablePath = NULL;
}
- (void)drawMapRect:(MKMapRect)mapRect
zoomScale:(MKZoomScale)zoomScale
inContext:(CGContextRef)context
{
CGFloat baseWidth = self.lineWidth;
CGFloat width = zoomScale < 0.01 ? baseWidth / 2 : baseWidth;
// nice for debugging
// CGContextSetRGBFillColor(context, (rand() % 255) / 255.0, 0, 0, 0.1);
// CGContextFillRect(context, [self rectForMapRect:mapRect]);
// draw the border. it's slightly wider than the specified line width.
[self drawLine:self.borderColor.CGColor
width:width * self.borderMultiplier
allowDashes:NO
forZoomScale:zoomScale
inContext:context];
// a white background.
if (self.backgroundColor) {
[self drawLine:self.backgroundColor.CGColor
width:width
allowDashes:NO
forZoomScale:zoomScale
inContext:context];
}
// draw the actual line.
[self drawLine:self.strokeColor.CGColor
width:width
allowDashes:YES
forZoomScale:zoomScale
inContext:context];
}
- (void)constructPath
{
// turn the polyline into a path
BOOL pathIsEmpty = YES;
for (int i = 0; i < self.polyline.pointCount; i++) {
CGPoint point = [self pointForMapPoint:self.polyline.points[i]];
if (pathIsEmpty) {
CGPathMoveToPoint(_mutablePath, nil, point.x, point.y);
pathIsEmpty = NO;
} else {
CGPathAddLineToPoint(_mutablePath, nil, point.x, point.y);
}
}
}
#pragma mark - Private helpers
- (void)drawLine:(CGColorRef)color
width:(CGFloat)width
allowDashes:(BOOL)allowDashes
forZoomScale:(MKZoomScale)zoomScale
inContext:(CGContextRef)context
{
CGContextAddPath(context, _mutablePath);
// use the defaults which takes care of the dash pattern
// and other things
if (allowDashes) {
[self applyStrokePropertiesToContext:context atZoomScale:zoomScale];
} else {
// some setting we still want to apply
CGContextSetLineCap(context, self.lineCap);
CGContextSetLineJoin(context, self.lineJoin);
CGContextSetMiterLimit(context, self.miterLimit);
}
// now set the colour and width
CGContextSetStrokeColorWithColor(context, color);
CGContextSetLineWidth(context, width / zoomScale);
CGContextStrokePath(context);
}
@end