summaryrefslogtreecommitdiff
path: root/platform/ios/MGLUserLocationAnnotationView.m
blob: 18c98bf1d0c05a65ec9fee96214d73765186ebfa (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#import "MGLUserLocationAnnotationView.h"

#import "MGLUserLocation_Private.h"
#import "MGLAnnotation.h"
#import "MGLMapView.h"

const CGFloat MGLTrackingDotRingWidth = 24.0;

@interface MGLUserLocationAnnotationView ()

@property (nonatomic, readwrite) CALayer *haloLayer;

@end

#pragma mark -

@implementation MGLUserLocationAnnotationView
{
    CALayer *_accuracyRingLayer;
    CALayer *_dotBorderLayer;
    CALayer *_dotLayer;
}

- (instancetype)initInMapView:(MGLMapView *)mapView
{
    if (self = [super init])
    {
        self.annotation = [[MGLUserLocation alloc] init];
        _mapView = mapView;
        [self setupLayers];
    }
    return self;
}

- (void)setTintColor:(UIColor *)tintColor
{
    UIImage *trackingDotHaloImage = [self trackingDotHaloImage];
    _haloLayer.bounds = CGRectMake(0, 0, trackingDotHaloImage.size.width, trackingDotHaloImage.size.height);
    _haloLayer.contents = (__bridge id)[trackingDotHaloImage CGImage];
    
    UIImage *dotImage = [self dotImage];
    _dotLayer.bounds = CGRectMake(0, 0, dotImage.size.width, dotImage.size.height);
    _dotLayer.contents = (__bridge id)[dotImage CGImage];
}

- (void)setupLayers
{
    if (CLLocationCoordinate2DIsValid(self.annotation.coordinate))
    {
        if ( ! _accuracyRingLayer && self.annotation.location.horizontalAccuracy)
        {
            UIImage *accuracyRingImage = [self accuracyRingImage];
            _accuracyRingLayer = [CALayer layer];
            _haloLayer.bounds = CGRectMake(0, 0, accuracyRingImage.size.width, accuracyRingImage.size.height);
            _haloLayer.contents = (__bridge id)[accuracyRingImage CGImage];
            _haloLayer.position = CGPointMake(super.layer.bounds.size.width / 2.0, super.layer.bounds.size.height / 2.0);
            
            [self.layer addSublayer:_accuracyRingLayer];
        }
        
        if ( ! _haloLayer)
        {
            UIImage *haloImage = [self trackingDotHaloImage];
            _haloLayer = [CALayer layer];
            _haloLayer.bounds = CGRectMake(0, 0, haloImage.size.width, haloImage.size.height);
            _haloLayer.contents = (__bridge id)[haloImage CGImage];
            _haloLayer.position = CGPointMake(super.layer.bounds.size.width / 2.0, super.layer.bounds.size.height / 2.0);
            
            [CATransaction begin];
            
            [CATransaction setAnimationDuration:3.5];
            [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
            
            // scale out radially
            //
            CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
            boundsAnimation.repeatCount = MAXFLOAT;
            boundsAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
            boundsAnimation.toValue   = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
            boundsAnimation.removedOnCompletion = NO;
            
            [_haloLayer addAnimation:boundsAnimation forKey:@"animateScale"];
            
            // go transparent as scaled out
            //
            CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
            opacityAnimation.repeatCount = MAXFLOAT;
            opacityAnimation.fromValue = [NSNumber numberWithFloat:1.0];
            opacityAnimation.toValue   = [NSNumber numberWithFloat:-1.0];
            opacityAnimation.removedOnCompletion = NO;
            
            [_haloLayer addAnimation:opacityAnimation forKey:@"animateOpacity"];
            
            [CATransaction commit];
            
            [self.layer addSublayer:_haloLayer];
        }
        
        // white dot background with shadow
        //
        if ( ! _dotBorderLayer)
        {
            CGRect rect = CGRectMake(0, 0, MGLTrackingDotRingWidth * 1.25, MGLTrackingDotRingWidth * 1.25);
            
            UIGraphicsBeginImageContextWithOptions(rect.size, NO, [[UIScreen mainScreen] scale]);
            CGContextRef context = UIGraphicsGetCurrentContext();
            
            CGContextSetShadow(context, CGSizeMake(0, 0), MGLTrackingDotRingWidth / 4.0);
            
            CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
            CGContextFillEllipseInRect(context, CGRectMake((rect.size.width - MGLTrackingDotRingWidth) / 2.0, (rect.size.height - MGLTrackingDotRingWidth) / 2.0, MGLTrackingDotRingWidth, MGLTrackingDotRingWidth));
            
            UIImage *whiteBackground = UIGraphicsGetImageFromCurrentImageContext();
            
            UIGraphicsEndImageContext();
            
            _dotBorderLayer = [CALayer layer];
            _dotBorderLayer.bounds = CGRectMake(0, 0, whiteBackground.size.width, whiteBackground.size.height);
            _dotBorderLayer.contents = (__bridge id)[whiteBackground CGImage];
            _dotBorderLayer.position = CGPointMake(super.layer.bounds.size.width / 2.0, super.layer.bounds.size.height / 2.0);
            [self.layer addSublayer:_dotBorderLayer];
        }

        // pulsing, tinted dot sublayer
        //
        if ( ! _dotLayer)
        {
            UIImage *dotImage = [self dotImage];
            _dotLayer = [CALayer layer];
            _dotLayer.bounds = CGRectMake(0, 0, dotImage.size.width, dotImage.size.height);
            _dotLayer.contents = (__bridge id)[dotImage CGImage];
            _dotLayer.position = CGPointMake(super.layer.bounds.size.width / 2.0, super.layer.bounds.size.height / 2.0);
            
            CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
            animation.repeatCount = MAXFLOAT;
            animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)];
            animation.toValue   = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)];
            animation.removedOnCompletion = NO;
            animation.autoreverses = YES;
            animation.duration = 1.5;
            animation.beginTime = CACurrentMediaTime() + 1.0;
            animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
            
            [_dotLayer addAnimation:animation forKey:@"animateTransform"];
            
            [self.layer addSublayer:_dotLayer];
        }
    }
}

- (UIImage *)accuracyRingImage
{
    CGFloat latRadians = self.annotation.coordinate.latitude * M_PI / 180.0f;
    CGFloat pixelRadius = self.annotation.location.horizontalAccuracy / cos(latRadians) / [self.mapView metersPerPixelAtLatitude:self.annotation.coordinate.latitude];
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(pixelRadius * 2, pixelRadius * 2), NO, [[UIScreen mainScreen] scale]);
    
    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor colorWithRed:0.378 green:0.552 blue:0.827 alpha:0.7] CGColor]);
    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor colorWithRed:0.378 green:0.552 blue:0.827 alpha:0.15] CGColor]);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
    CGContextStrokeEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, pixelRadius * 2, pixelRadius * 2));
    
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return finalImage;
}

- (UIImage *)trackingDotHaloImage
{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), NO, [[UIScreen mainScreen] scale]);
    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [[_mapView.tintColor colorWithAlphaComponent:0.75] CGColor]);
    CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, 100, 100));
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return finalImage;
}

- (UIImage *)dotImage
{
    CGFloat tintedWidth = MGLTrackingDotRingWidth * 0.7;
    
    CGRect rect = CGRectMake(0, 0, tintedWidth, tintedWidth);
    
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, [[UIScreen mainScreen] scale]);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [_mapView.tintColor CGColor]);
    CGContextFillEllipseInRect(context, CGRectMake((rect.size.width - tintedWidth) / 2.0, (rect.size.height - tintedWidth) / 2.0, tintedWidth, tintedWidth));
    
    UIImage *tintedForeground = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return tintedForeground;
}

@end