-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
AIRMap.m
669 lines (574 loc) · 22.9 KB
/
AIRMap.m
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "AIRMap.h"
#import <React/RCTEventDispatcher.h>
#import <React/UIView+React.h>
#import "AIRMapMarker.h"
#import "AIRMapPolyline.h"
#import "AIRMapPolygon.h"
#import "AIRMapCircle.h"
#import <QuartzCore/QuartzCore.h>
#import "AIRMapUrlTile.h"
#import "AIRMapWMSTile.h"
#import "AIRMapLocalTile.h"
#import "AIRMapOverlay.h"
const CLLocationDegrees AIRMapDefaultSpan = 0.005;
const NSTimeInterval AIRMapRegionChangeObserveInterval = 0.1;
const CGFloat AIRMapZoomBoundBuffer = 0.01;
const NSInteger AIRMapMaxZoomLevel = 20;
@interface MKMapView (UIGestureRecognizer)
// this tells the compiler that MKMapView actually implements this method
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
@end
@interface AIRMap ()
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
@property (nonatomic, assign) NSNumber *shouldZoomEnabled;
@property (nonatomic, assign) NSNumber *shouldScrollEnabled;
- (void)updateScrollEnabled;
- (void)updateZoomEnabled;
@end
@implementation AIRMap
{
UIView *_legalLabel;
CLLocationManager *_locationManager;
BOOL _initialRegionSet;
BOOL _initialCameraSet;
// Array to manually track RN subviews
//
// AIRMap implicitly creates subviews that aren't regular RN children
// (SMCalloutView injects an overlay subview), which otherwise confuses RN
// during component re-renders:
// https://github.com/facebook/react-native/blob/v0.16.0/React/Modules/RCTUIManager.m#L657
//
// Implementation based on RCTTextField, another component with indirect children
// https://github.com/facebook/react-native/blob/v0.16.0/Libraries/Text/RCTTextField.m#L20
NSMutableArray<UIView *> *_reactSubviews;
}
- (instancetype)init
{
if ((self = [super init])) {
_hasStartedRendering = NO;
_reactSubviews = [NSMutableArray new];
// Find Apple link label
for (UIView *subview in self.subviews) {
if ([NSStringFromClass(subview.class) isEqualToString:@"MKAttributionLabel"]) {
// This check is super hacky, but the whole premise of moving around
// Apple's internal subviews is super hacky
_legalLabel = subview;
break;
}
}
// 3rd-party callout view for MapKit that has more options than the built-in. It's painstakingly built to
// be identical to the built-in callout view (which has a private API)
self.calloutView = [SMCalloutView platformCalloutView];
self.calloutView.delegate = self;
self.minZoomLevel = 0;
self.maxZoomLevel = AIRMapMaxZoomLevel;
self.compassOffset = CGPointMake(0, 0);
}
return self;
}
- (void)dealloc
{
[_regionChangeObserveTimer invalidate];
}
-(void)addSubview:(UIView *)view {
if([view isKindOfClass:[AIRMapMarker class]]) {
[self addAnnotation:(id <MKAnnotation>)view];
} else {
[super addSubview:view];
}
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-missing-super-calls"
- (void)insertReactSubview:(id<RCTComponent>)subview atIndex:(NSInteger)atIndex {
// Our desired API is to pass up markers/overlays as children to the mapview component.
// This is where we intercept them and do the appropriate underlying mapview action.
if ([subview isKindOfClass:[AIRMapMarker class]]) {
[self addAnnotation:(id <MKAnnotation>) subview];
} else if ([subview isKindOfClass:[AIRMapPolyline class]]) {
((AIRMapPolyline *)subview).map = self;
[self addOverlay:(id<MKOverlay>)subview];
} else if ([subview isKindOfClass:[AIRMapPolygon class]]) {
((AIRMapPolygon *)subview).map = self;
[self addOverlay:(id<MKOverlay>)subview];
} else if ([subview isKindOfClass:[AIRMapCircle class]]) {
((AIRMapCircle *)subview).map = self;
[self addOverlay:(id<MKOverlay>)subview];
} else if ([subview isKindOfClass:[AIRMapUrlTile class]]) {
((AIRMapUrlTile *)subview).map = self;
[self addOverlay:(id<MKOverlay>)subview];
}else if ([subview isKindOfClass:[AIRMapWMSTile class]]) {
((AIRMapWMSTile *)subview).map = self;
[self addOverlay:(id<MKOverlay>)subview];
} else if ([subview isKindOfClass:[AIRMapLocalTile class]]) {
((AIRMapLocalTile *)subview).map = self;
[self addOverlay:(id<MKOverlay>)subview];
} else if ([subview isKindOfClass:[AIRMapOverlay class]]) {
((AIRMapOverlay *)subview).map = self;
[self addOverlay:(id<MKOverlay>)subview];
} else {
NSArray<id<RCTComponent>> *childSubviews = [subview reactSubviews];
for (int i = 0; i < childSubviews.count; i++) {
[self insertReactSubview:(UIView *)childSubviews[i] atIndex:atIndex];
}
}
[_reactSubviews insertObject:(UIView *)subview atIndex:(NSUInteger) atIndex];
}
#pragma clang diagnostic pop
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-missing-super-calls"
- (void)removeReactSubview:(id<RCTComponent>)subview {
// similarly, when the children are being removed we have to do the appropriate
// underlying mapview action here.
if ([subview isKindOfClass:[AIRMapMarker class]]) {
[self removeAnnotation:(id<MKAnnotation>)subview];
} else if ([subview isKindOfClass:[AIRMapPolyline class]]) {
[self removeOverlay:(id <MKOverlay>) subview];
} else if ([subview isKindOfClass:[AIRMapPolygon class]]) {
[self removeOverlay:(id <MKOverlay>) subview];
} else if ([subview isKindOfClass:[AIRMapCircle class]]) {
[self removeOverlay:(id <MKOverlay>) subview];
} else if ([subview isKindOfClass:[AIRMapUrlTile class]]) {
[self removeOverlay:(id <MKOverlay>) subview];
} else if ([subview isKindOfClass:[AIRMapLocalTile class]]) {
[self removeOverlay:(id <MKOverlay>) subview];
} else if ([subview isKindOfClass:[AIRMapOverlay class]]) {
[self removeOverlay:(id <MKOverlay>) subview];
} else {
NSArray<id<RCTComponent>> *childSubviews = [subview reactSubviews];
for (int i = 0; i < childSubviews.count; i++) {
[self removeReactSubview:(UIView *)childSubviews[i]];
}
}
[_reactSubviews removeObject:(UIView *)subview];
}
#pragma clang diagnostic pop
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-missing-super-calls"
- (NSArray<id<RCTComponent>> *)reactSubviews {
return _reactSubviews;
}
#pragma clang diagnostic pop
#pragma mark Utils
- (NSArray*) markers {
NSPredicate *filterMarkers = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
AIRMapMarker *marker = (AIRMapMarker *)evaluatedObject;
return [marker isKindOfClass:[AIRMapMarker class]];
}];
NSArray *filteredMarkers = [self.annotations filteredArrayUsingPredicate:filterMarkers];
return filteredMarkers;
}
- (AIRMapMarker*) markerForCallout:(AIRMapCallout*)callout {
AIRMapMarker* marker = nil;
NSArray* markers = [self markers];
for (AIRMapMarker* mrk in markers) {
if (mrk.calloutView == callout) {
marker = mrk;
break;
}
}
return marker;
}
- (CGRect) frameForMarker:(AIRMapMarker*) mrkAnn {
MKAnnotationView* mrkView = [self viewForAnnotation: mrkAnn];
CGRect mrkFrame = mrkView.frame;
return mrkFrame;
}
- (NSDictionary*) getMarkersFramesWithOnlyVisible:(BOOL)onlyVisible {
NSMutableDictionary* markersFrames = [NSMutableDictionary new];
for (AIRMapMarker* mrkAnn in self.markers) {
CGRect frame = [self frameForMarker:mrkAnn];
CGPoint point = [self convertCoordinate:mrkAnn.coordinate toPointToView:self];
NSDictionary* frameDict = @{
@"x": @(frame.origin.x),
@"y": @(frame.origin.y),
@"width": @(frame.size.width),
@"height": @(frame.size.height)
};
NSDictionary* pointDict = @{
@"x": @(point.x),
@"y": @(point.y)
};
NSString* k = mrkAnn.identifier;
BOOL isVisible = CGRectIntersectsRect(self.bounds, frame);
if (k != nil && (!onlyVisible || isVisible)) {
[markersFrames setObject:@{ @"frame": frameDict, @"point": pointDict } forKey:k];
}
}
return markersFrames;
}
- (AIRMapMarker*) markerAtPoint:(CGPoint)point {
AIRMapMarker* mrk = nil;
for (AIRMapMarker* mrkAnn in self.markers) {
CGRect frame = [self frameForMarker:mrkAnn];
if (CGRectContainsPoint(frame, point)) {
mrk = mrkAnn;
break;
}
}
return mrk;
}
#pragma mark Overrides for Callout behavior
// override UIGestureRecognizer's delegate method so we can prevent MKMapView's recognizer from firing
// when we interact with UIControl subclasses inside our callout view.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isDescendantOfView:self.calloutView])
return NO;
else
return [super gestureRecognizer:gestureRecognizer shouldReceiveTouch:touch];
}
// Allow touches to be sent to our calloutview.
// See this for some discussion of why we need to override this: https://github.com/nfarina/calloutview/pull/9
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CGPoint touchPoint = [self.calloutView convertPoint:point fromView:self];
UIView *touchedView = [self.calloutView hitTest:touchPoint withEvent:event];
if (touchedView) {
UIWindow* win = [[[UIApplication sharedApplication] windows] firstObject];
AIRMapCalloutSubview* calloutSubview = nil;
AIRMapCallout* callout = nil;
AIRMapMarker* marker = nil;
UIView* tmp = touchedView;
while (tmp && tmp != win && tmp != self.calloutView) {
if ([tmp respondsToSelector:@selector(onPress)]) {
calloutSubview = (AIRMapCalloutSubview*) tmp;
}
if ([tmp isKindOfClass:[AIRMapCallout class]]) {
callout = (AIRMapCallout*) tmp;
break;
}
tmp = tmp.superview;
}
if (callout) {
marker = [self markerForCallout:callout];
if (marker) {
CGPoint touchPointReal = [marker.calloutView convertPoint:point fromView:self];
if (![callout isPointInside:touchPointReal]) {
return [super hitTest:point withEvent:event];
}
}
}
return calloutSubview ? calloutSubview : touchedView;
}
return [super hitTest:point withEvent:event];
}
#pragma mark SMCalloutViewDelegate
- (NSTimeInterval)calloutView:(SMCalloutView *)calloutView delayForRepositionWithSize:(CGSize)offset {
// When the callout is being asked to present in a way where it or its target will be partially offscreen, it asks us
// if we'd like to reposition our surface first so the callout is completely visible. Here we scroll the map into view,
// but it takes some math because we have to deal in lon/lat instead of the given offset in pixels.
CLLocationCoordinate2D coordinate = self.region.center;
// where's the center coordinate in terms of our view?
CGPoint center = [self convertCoordinate:coordinate toPointToView:self];
// move it by the requested offset
center.x -= offset.width;
center.y -= offset.height;
// and translate it back into map coordinates
coordinate = [self convertPoint:center toCoordinateFromView:self];
// move the map!
[self setCenterCoordinate:coordinate animated:YES];
// tell the callout to wait for a while while we scroll (we assume the scroll delay for MKMapView matches UIScrollView)
return kSMCalloutViewRepositionDelayForUIScrollView;
}
#pragma mark Accessors
- (NSArray *)getMapBoundaries
{
MKMapRect mapRect = self.visibleMapRect;
CLLocationCoordinate2D northEast = MKCoordinateForMapPoint(MKMapPointMake(MKMapRectGetMaxX(mapRect), mapRect.origin.y));
CLLocationCoordinate2D southWest = MKCoordinateForMapPoint(MKMapPointMake(mapRect.origin.x, MKMapRectGetMaxY(mapRect)));
return @[
@[
[NSNumber numberWithDouble:northEast.longitude],
[NSNumber numberWithDouble:northEast.latitude]
],
@[
[NSNumber numberWithDouble:southWest.longitude],
[NSNumber numberWithDouble:southWest.latitude]
]
];
}
- (void)setShowsUserLocation:(BOOL)showsUserLocation
{
if (self.showsUserLocation != showsUserLocation) {
if (showsUserLocation && !_locationManager) {
_locationManager = [CLLocationManager new];
if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[_locationManager requestWhenInUseAuthorization];
}
}
super.showsUserLocation = showsUserLocation;
}
}
- (void)setTintColor:(UIColor *)tintColor
{
super.tintColor = tintColor;
}
- (void)setFollowsUserLocation:(BOOL)followsUserLocation
{
_followUserLocation = followsUserLocation;
}
- (void)setHandlePanDrag:(BOOL)handleMapDrag {
for (UIGestureRecognizer *recognizer in [self gestureRecognizers]) {
if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
recognizer.enabled = handleMapDrag;
break;
}
}
}
- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated
{
// If location is invalid, abort
if (!CLLocationCoordinate2DIsValid(region.center)) {
return;
}
// If new span values are nil, use old values instead
if (!region.span.latitudeDelta) {
region.span.latitudeDelta = self.region.span.latitudeDelta;
}
if (!region.span.longitudeDelta) {
region.span.longitudeDelta = self.region.span.longitudeDelta;
}
// Animate/move to new position
[super setRegion:region animated:animated];
}
- (void)setInitialRegion:(MKCoordinateRegion)initialRegion {
if (!_initialRegionSet) {
_initialRegionSet = YES;
[self setRegion:initialRegion animated:NO];
}
}
- (void)setCamera:(MKMapCamera*)camera animated:(BOOL)animated
{
[super setCamera:camera animated:animated];
}
- (void)setInitialCamera:(MKMapCamera*)initialCamera {
if (!_initialCameraSet) {
_initialCameraSet = YES;
[self setCamera:initialCamera animated:NO];
}
}
- (void)setCacheEnabled:(BOOL)cacheEnabled {
_cacheEnabled = cacheEnabled;
if (self.cacheEnabled && self.cacheImageView.image == nil) {
self.loadingView.hidden = NO;
[self.activityIndicatorView startAnimating];
}
else {
if (_loadingView != nil) {
self.loadingView.hidden = YES;
}
}
}
- (void)setLoadingEnabled:(BOOL)loadingEnabled {
_loadingEnabled = loadingEnabled;
if (!self.hasShownInitialLoading) {
self.loadingView.hidden = !self.loadingEnabled;
}
else {
if (_loadingView != nil) {
self.loadingView.hidden = YES;
}
}
}
- (UIColor *)loadingBackgroundColor {
return self.loadingView.backgroundColor;
}
- (void)setLoadingBackgroundColor:(UIColor *)loadingBackgroundColor {
self.loadingView.backgroundColor = loadingBackgroundColor;
}
- (UIColor *)loadingIndicatorColor {
return self.activityIndicatorView.color;
}
- (void)setLoadingIndicatorColor:(UIColor *)loadingIndicatorColor {
self.activityIndicatorView.color = loadingIndicatorColor;
}
// Include properties of MKMapView which are only available on iOS 9+
// and check if their selector is available before calling super method.
- (void)setShowsCompass:(BOOL)showsCompass {
if ([MKMapView instancesRespondToSelector:@selector(setShowsCompass:)]) {
[super setShowsCompass:showsCompass];
}
}
- (BOOL)showsCompass {
if ([MKMapView instancesRespondToSelector:@selector(showsCompass)]) {
return [super showsCompass];
} else {
return NO;
}
}
- (void)setShowsScale:(BOOL)showsScale {
if ([MKMapView instancesRespondToSelector:@selector(setShowsScale:)]) {
[super setShowsScale:showsScale];
}
}
- (BOOL)showsScale {
if ([MKMapView instancesRespondToSelector:@selector(showsScale)]) {
return [super showsScale];
} else {
return NO;
}
}
- (void)setShowsTraffic:(BOOL)showsTraffic {
if ([MKMapView instancesRespondToSelector:@selector(setShowsTraffic:)]) {
[super setShowsTraffic:showsTraffic];
}
}
- (BOOL)showsTraffic {
if ([MKMapView instancesRespondToSelector:@selector(showsTraffic)]) {
return [super showsTraffic];
} else {
return NO;
}
}
- (void)setScrollEnabled:(BOOL)scrollEnabled {
self.shouldScrollEnabled = [NSNumber numberWithBool:scrollEnabled];
[self updateScrollEnabled];
}
- (void)updateScrollEnabled {
if (self.cacheEnabled) {
[super setScrollEnabled:NO];
}
else if (self.shouldScrollEnabled != nil) {
[super setScrollEnabled:[self.shouldScrollEnabled boolValue]];
}
}
- (void)setZoomEnabled:(BOOL)zoomEnabled {
self.shouldZoomEnabled = [NSNumber numberWithBool:zoomEnabled];
[self updateZoomEnabled];
}
- (void)updateZoomEnabled {
if (self.cacheEnabled) {
[super setZoomEnabled: NO];
}
else if (self.shouldZoomEnabled != nil) {
[super setZoomEnabled:[self.shouldZoomEnabled boolValue]];
}
}
- (void)cacheViewIfNeeded {
// https://github.com/react-native-community/react-native-maps/issues/3100
// Do nothing if app is not active
if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
return;
}
if (self.hasShownInitialLoading) {
if (!self.cacheEnabled) {
if (_cacheImageView != nil) {
self.cacheImageView.hidden = YES;
self.cacheImageView.image = nil;
}
}
else {
self.cacheImageView.image = nil;
self.cacheImageView.hidden = YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.cacheImageView.image = nil;
self.cacheImageView.hidden = YES;
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.cacheImageView.image = image;
self.cacheImageView.hidden = NO;
});
}
[self updateScrollEnabled];
[self updateZoomEnabled];
[self updateLegalLabelInsets];
}
}
- (void)updateLegalLabelInsets {
if (_legalLabel) {
dispatch_async(dispatch_get_main_queue(), ^{
CGRect frame = self->_legalLabel.frame;
if (self->_legalLabelInsets.left) {
frame.origin.x = self->_legalLabelInsets.left;
} else if (self->_legalLabelInsets.right) {
frame.origin.x = self.frame.size.width - self->_legalLabelInsets.right - frame.size.width;
}
if (self->_legalLabelInsets.top) {
frame.origin.y = self->_legalLabelInsets.top;
} else if (self->_legalLabelInsets.bottom) {
frame.origin.y = self.frame.size.height - self->_legalLabelInsets.bottom - frame.size.height;
}
self->_legalLabel.frame = frame;
});
}
}
- (void)setLegalLabelInsets:(UIEdgeInsets)legalLabelInsets {
_legalLabelInsets = legalLabelInsets;
[self updateLegalLabelInsets];
}
- (void)setMapPadding:(UIEdgeInsets)mapPadding {
self.layoutMargins = mapPadding;
}
- (UIEdgeInsets)mapPadding {
return self.layoutMargins;
}
- (void)beginLoading {
if ((!self.hasShownInitialLoading && self.loadingEnabled) || (self.cacheEnabled && self.cacheImageView.image == nil)) {
self.loadingView.hidden = NO;
[self.activityIndicatorView startAnimating];
}
else {
if (_loadingView != nil) {
self.loadingView.hidden = YES;
}
}
}
- (void)finishLoading {
self.hasShownInitialLoading = YES;
if (_loadingView != nil) {
self.loadingView.hidden = YES;
}
[self cacheViewIfNeeded];
}
- (UIActivityIndicatorView *)activityIndicatorView {
if (_activityIndicatorView == nil) {
_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_activityIndicatorView.center = self.loadingView.center;
_activityIndicatorView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
_activityIndicatorView.color = [UIColor colorWithRed:96.f/255.f green:96.f/255.f blue:96.f/255.f alpha:1.f]; // defaults to #606060
}
[self.loadingView addSubview:_activityIndicatorView];
return _activityIndicatorView;
}
- (UIView *)loadingView {
if (_loadingView == nil) {
_loadingView = [[UIView alloc] initWithFrame:self.bounds];
_loadingView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
_loadingView.backgroundColor = [UIColor whiteColor]; // defaults to #FFFFFF
[self addSubview:_loadingView];
_loadingView.hidden = NO;
}
return _loadingView;
}
- (UIImageView *)cacheImageView {
if (_cacheImageView == nil) {
_cacheImageView = [[UIImageView alloc] initWithFrame:self.bounds];
_cacheImageView.contentMode = UIViewContentModeCenter;
_cacheImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self addSubview:self.cacheImageView];
_cacheImageView.hidden = YES;
}
return _cacheImageView;
}
- (void)layoutSubviews {
[super layoutSubviews];
[self cacheViewIfNeeded];
NSUInteger index = [[self subviews] indexOfObjectPassingTest:^BOOL(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *str = NSStringFromClass([obj class]);
return [str containsString:@"MKCompassView"];
}];
if (index != NSNotFound) {
UIView* compassButton;
compassButton = [self.subviews objectAtIndex:index];
compassButton.frame = CGRectMake(compassButton.frame.origin.x + _compassOffset.x, compassButton.frame.origin.y + _compassOffset.y, compassButton.frame.size.width, compassButton.frame.size.height);
}
}
@end