Skip to content

Commit 4efd881

Browse files
authored
fix: correctly check for iOS OS version before using cameraZoomRange (react-native-maps#5185)
fix: correctly check for iOS OS version before using cameraZoomRange
1 parent 37ca931 commit 4efd881

File tree

1 file changed

+71
-72
lines changed

1 file changed

+71
-72
lines changed

ios/AirMaps/AIRMap.m

Lines changed: 71 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ @implementation AIRMap
5151
UIView *_legalLabel;
5252
BOOL _initialRegionSet;
5353
BOOL _initialCameraSet;
54-
54+
5555
// Array to manually track RN subviews
5656
//
5757
// AIRMap implicitly creates subviews that aren't regular RN children
@@ -69,7 +69,7 @@ - (instancetype)init
6969
if ((self = [super init])) {
7070
_hasStartedRendering = NO;
7171
_reactSubviews = [NSMutableArray new];
72-
72+
7373
// Find Apple link label
7474
for (UIView *subview in self.subviews) {
7575
if ([NSStringFromClass(subview.class) isEqualToString:@"MKAttributionLabel"]) {
@@ -79,12 +79,12 @@ - (instancetype)init
7979
break;
8080
}
8181
}
82-
82+
8383
// 3rd-party callout view for MapKit that has more options than the built-in. It's painstakingly built to
8484
// be identical to the built-in callout view (which has a private API)
8585
self.calloutView = [SMCalloutView platformCalloutView];
8686
self.calloutView.delegate = self;
87-
87+
8888
self.minZoomLevel = 0;
8989
self.maxZoomLevel = AIRMapMaxZoomLevel;
9090
self.compassOffset = CGPointMake(0, 0);
@@ -132,7 +132,7 @@ - (void)insertReactSubview:(id<RCTComponent>)subview atIndex:(NSInteger)atIndex
132132
} else {
133133
NSArray<id<RCTComponent>> *childSubviews = [subview reactSubviews];
134134
for (int i = 0; i < childSubviews.count; i++) {
135-
[self insertReactSubview:(UIView *)childSubviews[i] atIndex:atIndex];
135+
[self insertReactSubview:(UIView *)childSubviews[i] atIndex:atIndex];
136136
}
137137
}
138138
[_reactSubviews insertObject:(UIView *)subview atIndex:(NSUInteger) atIndex];
@@ -163,7 +163,7 @@ - (void)removeReactSubview:(id<RCTComponent>)subview {
163163
} else {
164164
NSArray<id<RCTComponent>> *childSubviews = [subview reactSubviews];
165165
for (int i = 0; i < childSubviews.count; i++) {
166-
[self removeReactSubview:(UIView *)childSubviews[i]];
166+
[self removeReactSubview:(UIView *)childSubviews[i]];
167167
}
168168
}
169169
[_reactSubviews removeObject:(UIView *)subview];
@@ -173,7 +173,7 @@ - (void)removeReactSubview:(id<RCTComponent>)subview {
173173
#pragma clang diagnostic push
174174
#pragma clang diagnostic ignored "-Wobjc-missing-super-calls"
175175
- (NSArray<id<RCTComponent>> *)reactSubviews {
176-
return _reactSubviews;
176+
return _reactSubviews;
177177
}
178178
#pragma clang diagnostic pop
179179

@@ -212,15 +212,15 @@ - (NSDictionary*) getMarkersFramesWithOnlyVisible:(BOOL)onlyVisible {
212212
CGRect frame = [self frameForMarker:mrkAnn];
213213
CGPoint point = [self convertCoordinate:mrkAnn.coordinate toPointToView:self];
214214
NSDictionary* frameDict = @{
215-
@"x": @(frame.origin.x),
216-
@"y": @(frame.origin.y),
217-
@"width": @(frame.size.width),
218-
@"height": @(frame.size.height)
219-
};
215+
@"x": @(frame.origin.x),
216+
@"y": @(frame.origin.y),
217+
@"width": @(frame.size.width),
218+
@"height": @(frame.size.height)
219+
};
220220
NSDictionary* pointDict = @{
221-
@"x": @(point.x),
222-
@"y": @(point.y)
223-
};
221+
@"x": @(point.x),
222+
@"y": @(point.y)
223+
};
224224
NSString* k = mrkAnn.identifier;
225225
BOOL isVisible = CGRectIntersectsRect(self.bounds, frame);
226226
if (k != nil && (!onlyVisible || isVisible)) {
@@ -257,7 +257,7 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceive
257257
// Allow touches to be sent to our calloutview.
258258
// See this for some discussion of why we need to override this: https://github.com/nfarina/calloutview/pull/9
259259
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
260-
260+
261261
CGPoint touchPoint = [self.calloutView convertPoint:point fromView:self];
262262
UIView *touchedView = [self.calloutView hitTest:touchPoint withEvent:event];
263263

@@ -291,33 +291,33 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
291291

292292
return calloutSubview ? calloutSubview : touchedView;
293293
}
294-
294+
295295
return [super hitTest:point withEvent:event];
296296
}
297297

298298
#pragma mark SMCalloutViewDelegate
299299

300300
- (NSTimeInterval)calloutView:(SMCalloutView *)calloutView delayForRepositionWithSize:(CGSize)offset {
301-
301+
302302
// When the callout is being asked to present in a way where it or its target will be partially offscreen, it asks us
303303
// if we'd like to reposition our surface first so the callout is completely visible. Here we scroll the map into view,
304304
// but it takes some math because we have to deal in lon/lat instead of the given offset in pixels.
305-
305+
306306
CLLocationCoordinate2D coordinate = self.region.center;
307-
307+
308308
// where's the center coordinate in terms of our view?
309309
CGPoint center = [self convertCoordinate:coordinate toPointToView:self];
310-
310+
311311
// move it by the requested offset
312312
center.x -= offset.width;
313313
center.y -= offset.height;
314-
314+
315315
// and translate it back into map coordinates
316316
coordinate = [self convertPoint:center toCoordinateFromView:self];
317-
317+
318318
// move the map!
319319
[self setCenterCoordinate:coordinate animated:YES];
320-
320+
321321
// tell the callout to wait for a while while we scroll (we assume the scroll delay for MKMapView matches UIScrollView)
322322
return kSMCalloutViewRepositionDelayForUIScrollView;
323323
}
@@ -330,7 +330,7 @@ - (NSArray *)getMapBoundaries
330330

331331
CLLocationCoordinate2D northEast = MKCoordinateForMapPoint(MKMapPointMake(MKMapRectGetMaxX(mapRect), mapRect.origin.y));
332332
CLLocationCoordinate2D southWest = MKCoordinateForMapPoint(MKMapPointMake(mapRect.origin.x, MKMapRectGetMaxY(mapRect)));
333-
333+
334334
return @[
335335
@[
336336
[NSNumber numberWithDouble:northEast.longitude],
@@ -395,15 +395,15 @@ - (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated
395395
if (!CLLocationCoordinate2DIsValid(region.center)) {
396396
return;
397397
}
398-
398+
399399
// If new span values are nil, use old values instead
400400
if (!region.span.latitudeDelta) {
401401
region.span.latitudeDelta = self.region.span.latitudeDelta;
402402
}
403403
if (!region.span.longitudeDelta) {
404404
region.span.longitudeDelta = self.region.span.longitudeDelta;
405405
}
406-
406+
407407
// Animate/move to new position
408408
[super setRegion:region animated:animated];
409409
}
@@ -470,40 +470,39 @@ - (void)setLoadingIndicatorColor:(UIColor *)loadingIndicatorColor {
470470
}
471471

472472
- (void)setCameraZoomRange:(NSDictionary *)cameraZoomRange {
473-
if (!@available(iOS 13.0, *)) {
474-
return;
475-
}
476-
477-
if (cameraZoomRange == nil) {
478-
cameraZoomRange = @{};
479-
}
480-
481-
NSNumber *minValue = cameraZoomRange[@"minCenterCoordinateDistance"];
482-
NSNumber *maxValue = cameraZoomRange[@"maxCenterCoordinateDistance"];
483-
484-
if (minValue == nil && maxValue == nil) {
485-
self.legacyZoomConstraintsEnabled = YES;
486-
487-
MKMapCameraZoomRange *defaultZoomRange = [[MKMapCameraZoomRange alloc] initWithMinCenterCoordinateDistance:MKMapCameraZoomDefault maxCenterCoordinateDistance:MKMapCameraZoomDefault];
488-
[super setCameraZoomRange:defaultZoomRange animated:NO];
489-
490-
return;
491-
}
492-
493-
MKMapCameraZoomRange *zoomRange = nil;
494-
495-
if (minValue != nil && maxValue != nil) {
496-
zoomRange = [[MKMapCameraZoomRange alloc] initWithMinCenterCoordinateDistance:[minValue doubleValue] maxCenterCoordinateDistance:[maxValue doubleValue]];
497-
} else if (minValue != nil) {
498-
zoomRange = [[MKMapCameraZoomRange alloc] initWithMinCenterCoordinateDistance:[minValue doubleValue]];
499-
} else if (maxValue != nil) {
500-
zoomRange = [[MKMapCameraZoomRange alloc] initWithMaxCenterCoordinateDistance:[maxValue doubleValue]];
473+
if (@available(iOS 13.0, *)) {
474+
475+
if (cameraZoomRange == nil) {
476+
cameraZoomRange = @{};
477+
}
478+
479+
NSNumber *minValue = cameraZoomRange[@"minCenterCoordinateDistance"];
480+
NSNumber *maxValue = cameraZoomRange[@"maxCenterCoordinateDistance"];
481+
482+
if (minValue == nil && maxValue == nil) {
483+
self.legacyZoomConstraintsEnabled = YES;
484+
485+
MKMapCameraZoomRange *defaultZoomRange = [[MKMapCameraZoomRange alloc] initWithMinCenterCoordinateDistance:MKMapCameraZoomDefault maxCenterCoordinateDistance:MKMapCameraZoomDefault];
486+
[super setCameraZoomRange:defaultZoomRange animated:NO];
487+
488+
return;
489+
}
490+
491+
MKMapCameraZoomRange *zoomRange = nil;
492+
493+
if (minValue != nil && maxValue != nil) {
494+
zoomRange = [[MKMapCameraZoomRange alloc] initWithMinCenterCoordinateDistance:[minValue doubleValue] maxCenterCoordinateDistance:[maxValue doubleValue]];
495+
} else if (minValue != nil) {
496+
zoomRange = [[MKMapCameraZoomRange alloc] initWithMinCenterCoordinateDistance:[minValue doubleValue]];
497+
} else if (maxValue != nil) {
498+
zoomRange = [[MKMapCameraZoomRange alloc] initWithMaxCenterCoordinateDistance:[maxValue doubleValue]];
499+
}
500+
501+
BOOL animated = [cameraZoomRange[@"animated"] boolValue];
502+
503+
self.legacyZoomConstraintsEnabled = NO;
504+
[super setCameraZoomRange:zoomRange animated:animated];
501505
}
502-
503-
BOOL animated = [cameraZoomRange[@"animated"] boolValue];
504-
505-
self.legacyZoomConstraintsEnabled = NO;
506-
[super setCameraZoomRange:zoomRange animated:animated];
507506
}
508507

509508
// Include properties of MKMapView which are only available on iOS 9+
@@ -598,20 +597,20 @@ - (void)cacheViewIfNeeded {
598597
else {
599598
self.cacheImageView.image = nil;
600599
self.cacheImageView.hidden = YES;
601-
600+
602601
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
603602
self.cacheImageView.image = nil;
604603
self.cacheImageView.hidden = YES;
605604
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0);
606605
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
607606
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
608607
UIGraphicsEndImageContext();
609-
608+
610609
self.cacheImageView.image = image;
611610
self.cacheImageView.hidden = NO;
612611
});
613612
}
614-
613+
615614
[self updateScrollEnabled];
616615
[self updateZoomEnabled];
617616
[self updateLegalLabelInsets];
@@ -639,16 +638,16 @@ - (void)updateLegalLabelInsets {
639638

640639

641640
- (void)setLegalLabelInsets:(UIEdgeInsets)legalLabelInsets {
642-
_legalLabelInsets = legalLabelInsets;
643-
[self updateLegalLabelInsets];
641+
_legalLabelInsets = legalLabelInsets;
642+
[self updateLegalLabelInsets];
644643
}
645644

646645
- (void)setMapPadding:(UIEdgeInsets)mapPadding {
647-
self.layoutMargins = mapPadding;
646+
self.layoutMargins = mapPadding;
648647
}
649648

650649
- (UIEdgeInsets)mapPadding {
651-
return self.layoutMargins;
650+
return self.layoutMargins;
652651
}
653652

654653
- (void)beginLoading {
@@ -707,7 +706,7 @@ - (UIImageView *)cacheImageView {
707706
- (void)layoutSubviews {
708707
[super layoutSubviews];
709708
[self cacheViewIfNeeded];
710-
709+
711710
if (self.overlayCompassButton == nil) {
712711
for (UIView *subview in self.subviews) {
713712
if (![NSStringFromClass(subview.class) isEqualToString:@"MKPassThroughStackView"]) continue;
@@ -735,22 +734,22 @@ - (void)layoutSubviews {
735734
// based on https://medium.com/@dmytrobabych/getting-actual-rotation-and-zoom-level-for-mapkit-mkmapview-e7f03f430aa9
736735
- (CGFloat)getZoomLevel {
737736
CGFloat cameraAngle = self.camera.heading;
738-
737+
739738
if (cameraAngle > 270) {
740739
cameraAngle = 360 - cameraAngle;
741740
} else if (cameraAngle > 90) {
742741
cameraAngle = fabs(cameraAngle - 180);
743742
}
744-
743+
745744
CGFloat angleRad = M_PI * cameraAngle / 180; // map rotation in radians
746745
CGFloat width = self.frame.size.width;
747746
CGFloat height = self.frame.size.height;
748747
CGFloat heightOffset = 20; // the offset (status bar height) which is taken by MapKit into consideration to calculate visible area height
749-
748+
750749
// calculating Longitude span corresponding to normal (non-rotated) width
751750
CGFloat spanStraight = width * self.region.span.longitudeDelta / (width * cos(angleRad) + (height - heightOffset) * sin(angleRad));
752751
int normalizingFactor = 512;
753-
752+
754753
return log2(360 * ((width / normalizingFactor) / spanStraight));
755754
}
756755

0 commit comments

Comments
 (0)