summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Wray <jason@mapbox.com>2019-07-03 13:37:20 -0700
committerJason Wray <jason@mapbox.com>2019-07-09 16:09:31 -0700
commit08a4ab802a6591d64f9e6a9f517083093a4c1b08 (patch)
treef85cea5c55f84002dc599a6017d0768dfae3d6b1
parent18f7e4b84d761623a825498398ef58b79ee39ecd (diff)
downloadqtlocation-mapboxgl-08a4ab802a6591d64f9e6a9f517083093a4c1b08.tar.gz
WIP compass visibility
-rw-r--r--platform/darwin/src/MGLTypes.h12
-rw-r--r--platform/ios/app/MBXViewController.m2
-rw-r--r--platform/ios/ios.xcodeproj/project.pbxproj16
-rw-r--r--platform/ios/jazzy.yml1
-rw-r--r--platform/ios/src/MGLCompassButton.h26
-rw-r--r--platform/ios/src/MGLCompassButton.mm108
-rw-r--r--platform/ios/src/MGLMapView.h9
-rw-r--r--platform/ios/src/MGLMapView.mm83
-rw-r--r--platform/ios/src/MGLMapView_Private.h3
-rw-r--r--platform/ios/src/Mapbox.h2
-rw-r--r--platform/ios/src/UIImage+MGLAdditions.h6
-rw-r--r--platform/ios/src/UIImage+MGLAdditions.mm18
-rw-r--r--platform/ios/test/MGLCompassViewTests.m23
13 files changed, 224 insertions, 85 deletions
diff --git a/platform/darwin/src/MGLTypes.h b/platform/darwin/src/MGLTypes.h
index 491e24310f..b8354d2e83 100644
--- a/platform/darwin/src/MGLTypes.h
+++ b/platform/darwin/src/MGLTypes.h
@@ -119,4 +119,16 @@ NS_INLINE MGLTransition MGLTransitionMake(NSTimeInterval duration, NSTimeInterva
return transition;
}
+/**
+ Constants indicating the visibility of different map ornaments.
+ */
+typedef NS_ENUM(NSInteger, MGLOrnamentVisibility) {
+ /** A constant indicating that the ornament adapts to the current map state. */
+ MGLOrnamentVisibilityAdaptive,
+ /** A constant indicating that the ornament is always hidden. */
+ MGLOrnamentVisibilityHidden,
+ /** A constant indicating that the ornament is always visible. */
+ MGLOrnamentVisibilityVisible
+};
+
NS_ASSUME_NONNULL_END
diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m
index 2fb95e1b17..2036d35fbe 100644
--- a/platform/ios/app/MBXViewController.m
+++ b/platform/ios/app/MBXViewController.m
@@ -302,6 +302,8 @@ CLLocationCoordinate2D randomWorldCoordinate() {
}
}
}];
+
+ self.mapView.compassView.compassVisibility = MGLOrnamentVisibilityAdaptive;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
diff --git a/platform/ios/ios.xcodeproj/project.pbxproj b/platform/ios/ios.xcodeproj/project.pbxproj
index fbf7974be0..eb12c046e9 100644
--- a/platform/ios/ios.xcodeproj/project.pbxproj
+++ b/platform/ios/ios.xcodeproj/project.pbxproj
@@ -424,6 +424,11 @@
96E516FB20005A4000A02306 /* MGLUserLocationHeadingBeamLayer.h in Headers */ = {isa = PBXBuildFile; fileRef = 966FCF4A1F3A5C9200F2B6DE /* MGLUserLocationHeadingBeamLayer.h */; };
96E516FC20005A4400A02306 /* MGLUserLocationHeadingIndicator.h in Headers */ = {isa = PBXBuildFile; fileRef = 96F3F73B1F5711F1003E2D2C /* MGLUserLocationHeadingIndicator.h */; };
96E5170420005A6B00A02306 /* SMCalloutView.h in Headers */ = {isa = PBXBuildFile; fileRef = DA8848891CBB037E00AB86E3 /* SMCalloutView.h */; };
+ 96E6145622CC135200109F14 /* MGLCompassViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 96E6145522CC135200109F14 /* MGLCompassViewTests.m */; };
+ 96E6145922CC169000109F14 /* MGLCompassButton.h in Headers */ = {isa = PBXBuildFile; fileRef = 96E6145722CC169000109F14 /* MGLCompassButton.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ 96E6145A22CC169000109F14 /* MGLCompassButton.h in Headers */ = {isa = PBXBuildFile; fileRef = 96E6145722CC169000109F14 /* MGLCompassButton.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ 96E6145B22CC169000109F14 /* MGLCompassButton.mm in Sources */ = {isa = PBXBuildFile; fileRef = 96E6145822CC169000109F14 /* MGLCompassButton.mm */; };
+ 96E6145C22CC169000109F14 /* MGLCompassButton.mm in Sources */ = {isa = PBXBuildFile; fileRef = 96E6145822CC169000109F14 /* MGLCompassButton.mm */; };
96ED34DE22374C0900E9FCA9 /* MGLMapViewDirectionTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 96ED34DD22374C0900E9FCA9 /* MGLMapViewDirectionTests.mm */; };
96F3F73C1F57124B003E2D2C /* MGLUserLocationHeadingIndicator.h in Headers */ = {isa = PBXBuildFile; fileRef = 96F3F73B1F5711F1003E2D2C /* MGLUserLocationHeadingIndicator.h */; };
9C188C4F2242C95A0022FA55 /* MMEDate.m in Sources */ = {isa = PBXBuildFile; fileRef = 40834BBC1FE05D6E00C1BD0D /* MMEDate.m */; };
@@ -1132,6 +1137,9 @@
96E0272C1E57C7E5004B8E66 /* fr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/Localizable.strings; sourceTree = "<group>"; };
96E0272D1E57C7E6004B8E66 /* vi */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = vi; path = vi.lproj/Localizable.strings; sourceTree = "<group>"; };
96E0272E1E57C7E7004B8E66 /* pt-BR */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "pt-BR"; path = "pt-BR.lproj/Localizable.strings"; sourceTree = "<group>"; };
+ 96E6145522CC135200109F14 /* MGLCompassViewTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MGLCompassViewTests.m; sourceTree = "<group>"; };
+ 96E6145722CC169000109F14 /* MGLCompassButton.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLCompassButton.h; sourceTree = "<group>"; };
+ 96E6145822CC169000109F14 /* MGLCompassButton.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLCompassButton.mm; sourceTree = "<group>"; };
96ED34DD22374C0900E9FCA9 /* MGLMapViewDirectionTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLMapViewDirectionTests.mm; sourceTree = "<group>"; };
96F017292118FBAE00892778 /* MGLMapView_Experimental.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLMapView_Experimental.h; sourceTree = "<group>"; };
96F3F73B1F5711F1003E2D2C /* MGLUserLocationHeadingIndicator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLUserLocationHeadingIndicator.h; sourceTree = "<group>"; };
@@ -1658,6 +1666,8 @@
355ADFF91E9281C300F3939D /* Views */ = {
isa = PBXGroup;
children = (
+ 96E6145722CC169000109F14 /* MGLCompassButton.h */,
+ 96E6145822CC169000109F14 /* MGLCompassButton.mm */,
355ADFFB1E9281DA00F3939D /* MGLScaleBar.h */,
355ADFFC1E9281DA00F3939D /* MGLScaleBar.mm */,
);
@@ -2026,6 +2036,7 @@
DA35A2C31CCA9F8300E826B2 /* MGLClockDirectionFormatterTests.m */,
35D9DDE11DA25EEC00DAAD69 /* MGLCodingTests.mm */,
DA35A2C41CCA9F8300E826B2 /* MGLCompassDirectionFormatterTests.m */,
+ 96E6145522CC135200109F14 /* MGLCompassViewTests.m */,
DA35A2A91CCA058D00E826B2 /* MGLCoordinateFormatterTests.m */,
3598544C1E1D38AA00B29F84 /* MGLDistanceFormatterTests.m */,
6407D66F1E0085FD00F6A9C3 /* MGLDocumentationExampleTests.swift */,
@@ -2454,6 +2465,7 @@
74CB5EC3219B282500102936 /* MGLCircleStyleLayer_Private.h in Headers */,
DA8847F01CBAFA5100AB86E3 /* MGLAnnotation.h in Headers */,
400533011DB0862B0069F638 /* NSArray+MGLAdditions.h in Headers */,
+ 96E6145922CC169000109F14 /* MGLCompassButton.h in Headers */,
1F06668A1EC64F8E001C16D7 /* MGLLight.h in Headers */,
4049C29D1DB6CD6C00B3F799 /* MGLPointCollection.h in Headers */,
40CF6DBB1DAC3C6600A4D18B /* MGLShape_Private.h in Headers */,
@@ -2645,6 +2657,7 @@
9C6E283922A982670056B7BE /* MMEEventLogReportViewController.h in Headers */,
1F6A82A92138871900BA5B41 /* MGLLoggingConfiguration_Private.h in Headers */,
DABFB8661CBE99E500D62B32 /* MGLPointAnnotation.h in Headers */,
+ 96E6145A22CC169000109F14 /* MGLCompassButton.h in Headers */,
96E516E42000560B00A02306 /* MGLComputedShapeSource_Private.h in Headers */,
96E516E92000560B00A02306 /* MGLAnnotationImage_Private.h in Headers */,
96E516E52000560B00A02306 /* MGLOfflinePack_Private.h in Headers */,
@@ -3240,6 +3253,7 @@
40CFA6511D7875BB008103BD /* MGLShapeSourceTests.mm in Sources */,
DA35A2C51CCA9F8300E826B2 /* MGLClockDirectionFormatterTests.m in Sources */,
35B8E08C1D6C8B5100E768D2 /* MGLPredicateTests.mm in Sources */,
+ 96E6145622CC135200109F14 /* MGLCompassViewTests.m in Sources */,
96036A0620059BBA00510F3D /* MGLNSOrthographyAdditionsTests.m in Sources */,
1F95931D1E6DE2E900D5B294 /* MGLNSDateAdditionsTests.mm in Sources */,
DA695426215B1E76002041A4 /* MGLMapCameraTests.m in Sources */,
@@ -3336,6 +3350,7 @@
3557F7B21E1D27D300CCA5E6 /* MGLDistanceFormatter.m in Sources */,
9C6E282B22A981570056B7BE /* MGLMapboxEvents.m in Sources */,
1FCCEC36222605C400302E3B /* MGLSDKMetricsManager.m in Sources */,
+ 96E6145B22CC169000109F14 /* MGLCompassButton.mm in Sources */,
40834BE71FE05E1800C1BD0D /* MMEAPIClient.m in Sources */,
DA8848591CBAFB9800AB86E3 /* MGLMapView.mm in Sources */,
DA8848501CBAFB9800AB86E3 /* MGLAnnotationImage.m in Sources */,
@@ -3462,6 +3477,7 @@
1FCCEC37222605C400302E3B /* MGLSDKMetricsManager.m in Sources */,
9C6E282C22A981580056B7BE /* MGLMapboxEvents.m in Sources */,
DAA4E41F1CBB730400178DFB /* MGLMultiPoint.mm in Sources */,
+ 96E6145C22CC169000109F14 /* MGLCompassButton.mm in Sources */,
DD0902AA1DB1929D00C5BDCE /* MGLNetworkConfiguration.m in Sources */,
40834C041FE05E1800C1BD0D /* MMELocationManager.m in Sources */,
9C6E281F22A980AC0056B7BE /* CLLocation+MMEMobileEvents.m in Sources */,
diff --git a/platform/ios/jazzy.yml b/platform/ios/jazzy.yml
index d459e93613..f5d0040ea5 100644
--- a/platform/ios/jazzy.yml
+++ b/platform/ios/jazzy.yml
@@ -57,6 +57,7 @@ custom_categories:
children:
- MGLCalloutView
- MGLCalloutViewDelegate
+ - MGLCompassButton
- name: Location Updates
children:
- MGLLocationManager
diff --git a/platform/ios/src/MGLCompassButton.h b/platform/ios/src/MGLCompassButton.h
new file mode 100644
index 0000000000..f42d5f315d
--- /dev/null
+++ b/platform/ios/src/MGLCompassButton.h
@@ -0,0 +1,26 @@
+#import <UIKit/UIKit.h>
+#import <CoreLocation/CoreLocation.h>
+
+#import "MGLTypes.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+/**
+ A specialized view that displays the current compass heading for its associated map.
+ */
+@interface MGLCompassButton : UIImageView
+
+/**
+ The visibility of the compass button.
+
+ You can configure a compass button to be visible all the time or only when the compass heading changes.
+ */
+@property (nonatomic, assign) MGLOrnamentVisibility compassVisibility;
+
+// TODO: Make these private
++ (instancetype)compassButton;
+- (void)updateCompassWithDirection:(CLLocationDirection)direction;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/platform/ios/src/MGLCompassButton.mm b/platform/ios/src/MGLCompassButton.mm
new file mode 100644
index 0000000000..6d62793915
--- /dev/null
+++ b/platform/ios/src/MGLCompassButton.mm
@@ -0,0 +1,108 @@
+#import "MGLCompassButton.h"
+#import "MGLCompassDirectionFormatter.h"
+
+#import <Mapbox/MGLGeometry.h>
+
+#import "MGLMapView_Private.h"
+#import "UIImage+MGLAdditions.h"
+#import "NSBundle+MGLAdditions.h"
+
+#include <mbgl/math/wrap.hpp>
+
+@interface MGLCompassButton ()
+
+@property (nonatomic) MGLCompassDirectionFormatter *accessibilityCompassFormatter;
+
+@end
+
+@implementation MGLCompassButton
+
++ (instancetype)compassButton {
+ return [[MGLCompassButton alloc] init];
+}
+
+- (instancetype)init {
+ if (self = [super init]) {
+ [self commonInit];
+ }
+ return self;
+}
+
+- (void)commonInit {
+ self.image = self.compassImage;
+
+ self.compassVisibility = MGLOrnamentVisibilityAdaptive;
+
+ self.alpha = 0;
+ self.userInteractionEnabled = YES;
+ self.translatesAutoresizingMaskIntoConstraints = NO;
+
+ self.accessibilityTraits = UIAccessibilityTraitButton;
+ self.accessibilityLabel = NSLocalizedStringWithDefaultValue(@"COMPASS_A11Y_LABEL", nil, nil, @"Compass", @"Accessibility label");
+ self.accessibilityHint = NSLocalizedStringWithDefaultValue(@"COMPASS_A11Y_HINT", nil, nil, @"Rotates the map to face due north", @"Accessibility hint");
+
+ self.accessibilityCompassFormatter = [[MGLCompassDirectionFormatter alloc] init];
+ self.accessibilityCompassFormatter.unitStyle = NSFormattingUnitStyleLong;
+
+ [self sizeToFit];
+}
+
+- (UIImage *)compassImage {
+ UIImage *scaleImage = [UIImage mgl_resourceImageNamed:@"Compass"];
+ UIGraphicsBeginImageContextWithOptions(scaleImage.size, NO, UIScreen.mainScreen.scale);
+ [scaleImage drawInRect:{CGPointZero, scaleImage.size}];
+
+ NSAttributedString *north = [[NSAttributedString alloc] initWithString:NSLocalizedStringWithDefaultValue(@"COMPASS_NORTH", nil, nil, @"N", @"Compass abbreviation for north") attributes:@{
+ NSFontAttributeName: [UIFont systemFontOfSize:11 weight:UIFontWeightUltraLight],
+ NSForegroundColorAttributeName: [UIColor whiteColor],
+ }];
+ CGRect stringRect = CGRectMake((scaleImage.size.width - north.size.width) / 2,
+ scaleImage.size.height * 0.435,
+ north.size.width, north.size.height);
+ [north drawInRect:stringRect];
+
+ UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
+ UIGraphicsEndImageContext();
+ return image;
+}
+
+- (void)updateCompassWithDirection:(CLLocationDirection)direction
+{
+ CLLocationDirection plateDirection = mbgl::util::wrap(-direction, 0., 360.);
+ self.transform = CGAffineTransformMakeRotation(MGLRadiansFromDegrees(plateDirection));
+
+ self.isAccessibilityElement = direction > 0;
+ self.accessibilityValue = [self.accessibilityCompassFormatter stringFromDirection:direction];
+
+ switch (self.compassVisibility) {
+ case MGLOrnamentVisibilityAdaptive:
+ if (direction > 0 && self.alpha < 1) {
+ [self showCompass];
+ } else if (direction == 0 && self.alpha > 0) {
+ [self hideCompass];
+ }
+ break;
+ case MGLOrnamentVisibilityVisible:
+ [self showCompass];
+ break;
+ case MGLOrnamentVisibilityHidden:
+ [self hideCompass];
+ break;
+ }
+}
+
+- (void)showCompass {
+ [self animateToAlpha:1];
+}
+
+- (void)hideCompass {
+ [self animateToAlpha:0];
+}
+
+- (void)animateToAlpha:(CGFloat)alpha {
+ [UIView animateWithDuration:MGLAnimationDuration delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
+ self.alpha = alpha;
+ } completion:nil];
+}
+
+@end
diff --git a/platform/ios/src/MGLMapView.h b/platform/ios/src/MGLMapView.h
index 87c24cd97b..9fe9a6c10a 100644
--- a/platform/ios/src/MGLMapView.h
+++ b/platform/ios/src/MGLMapView.h
@@ -1,9 +1,9 @@
-#import "MGLGeometry.h"
-#import "MGLMapCamera.h"
-
#import <UIKit/UIKit.h>
+#import "MGLCompassButton.h"
#import "MGLFoundation.h"
+#import "MGLGeometry.h"
+#import "MGLMapCamera.h"
#import "MGLTypes.h"
NS_ASSUME_NONNULL_BEGIN
@@ -122,7 +122,6 @@ FOUNDATION_EXTERN MGL_EXPORT const MGLMapViewPreferredFramesPerSecond MGLMapView
FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLMissingLocationServicesUsageDescriptionException;
FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLUserLocationAnnotationTypeException;
-FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLResourceNotFoundException;
/**
An interactive, customizable map view with an interface similar to the one
@@ -316,7 +315,7 @@ MGL_EXPORT
A control indicating the map’s direction and allowing the user to manipulate
the direction, positioned in the upper-right corner.
*/
-@property (nonatomic, readonly) UIImageView *compassView;
+@property (nonatomic, readonly) MGLCompassButton *compassView;
/**
The position of the compass view. The default value is `MGLOrnamentPositionTopRight`.
diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm
index c0b7380607..ec3766c3a0 100644
--- a/platform/ios/src/MGLMapView.mm
+++ b/platform/ios/src/MGLMapView.mm
@@ -86,7 +86,6 @@ const MGLMapViewPreferredFramesPerSecond MGLMapViewPreferredFramesPerSecondMaxim
const MGLExceptionName MGLMissingLocationServicesUsageDescriptionException = @"MGLMissingLocationServicesUsageDescriptionException";
const MGLExceptionName MGLUserLocationAnnotationTypeException = @"MGLUserLocationAnnotationTypeException";
-const MGLExceptionName MGLResourceNotFoundException = @"MGLResourceNotFoundException";
const MGLExceptionName MGLUnderlyingMapUnavailableException = @"MGLUnderlyingMapUnavailableException";
const CGPoint MGLOrnamentDefaultPositionOffset = CGPointMake(8, 8);
@@ -197,7 +196,7 @@ public:
@property (nonatomic) NSMutableArray<NSLayoutConstraint *> *scaleBarConstraints;
@property (nonatomic, readwrite) MGLScaleBar *scaleBar;
-@property (nonatomic, readwrite) UIImageView *compassView;
+@property (nonatomic, readwrite) MGLCompassButton *compassView;
@property (nonatomic) NSMutableArray<NSLayoutConstraint *> *compassViewConstraints;
@property (nonatomic, readwrite) UIImageView *logoView;
@property (nonatomic) NSMutableArray<NSLayoutConstraint *> *logoViewConstraints;
@@ -308,7 +307,6 @@ public:
BOOL _delegateHasFillColorsForShapeAnnotations;
BOOL _delegateHasLineWidthsForShapeAnnotations;
- MGLCompassDirectionFormatter *_accessibilityCompassFormatter;
NSArray<id <MGLFeature>> *_visiblePlaceFeatures;
NSArray<id <MGLFeature>> *_visibleRoadFeatures;
NSMutableSet<MGLFeatureAccessibilityElement *> *_featureAccessibilityElements;
@@ -436,8 +434,6 @@ public:
// self.isAccessibilityElement = YES;
self.accessibilityLabel = NSLocalizedStringWithDefaultValue(@"MAP_A11Y_LABEL", nil, nil, @"Map", @"Accessibility label");
self.accessibilityTraits = UIAccessibilityTraitAllowsDirectInteraction | UIAccessibilityTraitAdjustable;
- _accessibilityCompassFormatter = [[MGLCompassDirectionFormatter alloc] init];
- _accessibilityCompassFormatter.unitStyle = NSFormattingUnitStyleLong;
self.backgroundColor = [UIColor clearColor];
self.clipsToBounds = YES;
if (@available(iOS 11.0, *)) { self.accessibilityIgnoresInvertColors = YES; }
@@ -510,7 +506,7 @@ public:
// setup logo
//
- UIImage *logo = [MGLMapView resourceImageNamed:@"mapbox"];
+ UIImage *logo = [UIImage mgl_resourceImageNamed:@"mapbox"];
_logoView = [[UIImageView alloc] initWithImage:logo];
_logoView.accessibilityTraits = UIAccessibilityTraitStaticText;
_logoView.accessibilityLabel = NSLocalizedStringWithDefaultValue(@"LOGO_A11Y_LABEL", nil, nil, @"Mapbox", @"Accessibility label");
@@ -538,14 +534,8 @@ public:
// setup compass
//
- _compassView = [[UIImageView alloc] initWithImage:self.compassImage];
- _compassView.alpha = 0;
- _compassView.userInteractionEnabled = YES;
+ _compassView = [MGLCompassButton compassButton];
[_compassView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleCompassTapGesture:)]];
- _compassView.accessibilityTraits = UIAccessibilityTraitButton;
- _compassView.accessibilityLabel = NSLocalizedStringWithDefaultValue(@"COMPASS_A11Y_LABEL", nil, nil, @"Compass", @"Accessibility label");
- _compassView.accessibilityHint = NSLocalizedStringWithDefaultValue(@"COMPASS_A11Y_HINT", nil, nil, @"Rotates the map to face due north", @"Accessibility hint");
- _compassView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_compassView];
_compassViewConstraints = [NSMutableArray array];
_compassViewPosition = MGLOrnamentPositionTopRight;
@@ -664,26 +654,6 @@ public:
static_cast<uint32_t>(size.height) };
}
-- (UIImage *)compassImage
-{
- UIImage *scaleImage = [MGLMapView resourceImageNamed:@"Compass"];
- UIGraphicsBeginImageContextWithOptions(scaleImage.size, NO, [UIScreen mainScreen].scale);
- [scaleImage drawInRect:{ CGPointZero, scaleImage.size }];
-
- NSAttributedString *north = [[NSAttributedString alloc] initWithString:NSLocalizedStringWithDefaultValue(@"COMPASS_NORTH", nil, nil, @"N", @"Compass abbreviation for north") attributes:@{
- NSFontAttributeName: [UIFont systemFontOfSize:11 weight:UIFontWeightUltraLight],
- NSForegroundColorAttributeName: [UIColor whiteColor],
- }];
- CGRect stringRect = CGRectMake((scaleImage.size.width - north.size.width) / 2,
- scaleImage.size.height * 0.435,
- north.size.width, north.size.height);
- [north drawInRect:stringRect];
-
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return image;
-}
-
- (void)reachabilityChanged:(NSNotification *)notification
{
MGLAssertIsMainThread();
@@ -4177,7 +4147,7 @@ public:
/// rect therefore excludes the bottom half.
- (MGLAnnotationImage *)defaultAnnotationImage
{
- UIImage *image = [MGLMapView resourceImageNamed:MGLDefaultStyleMarkerSymbolName];
+ UIImage *image = [UIImage mgl_resourceImageNamed:MGLDefaultStyleMarkerSymbolName];
image = [image imageWithAlignmentRectInsets:
UIEdgeInsetsMake(0, 0, image.size.height / 2, 0)];
MGLAnnotationImage *annotationImage = [MGLAnnotationImage annotationImageWithImage:image
@@ -6547,35 +6517,7 @@ public:
- (void)updateCompass
{
- CLLocationDirection direction = self.direction;
- CLLocationDirection plateDirection = mbgl::util::wrap(-direction, 0., 360.);
- self.compassView.transform = CGAffineTransformMakeRotation(MGLRadiansFromDegrees(plateDirection));
-
- self.compassView.isAccessibilityElement = direction > 0;
- self.compassView.accessibilityValue = [_accessibilityCompassFormatter stringFromDirection:direction];
-
- if (direction > 0 && self.compassView.alpha < 1)
- {
- [UIView animateWithDuration:MGLAnimationDuration
- delay:0
- options:UIViewAnimationOptionBeginFromCurrentState
- animations:^
- {
- self.compassView.alpha = 1;
- }
- completion:nil];
- }
- else if (direction == 0 && self.compassView.alpha > 0)
- {
- [UIView animateWithDuration:MGLAnimationDuration
- delay:0
- options:UIViewAnimationOptionBeginFromCurrentState
- animations:^
- {
- self.compassView.alpha = 0;
- }
- completion:nil];
- }
+ [self.compassView updateCompassWithDirection:self.direction];
}
- (void)updateScaleBar
@@ -6593,21 +6535,6 @@ public:
}
}
-+ (UIImage *)resourceImageNamed:(NSString *)imageName
-{
- UIImage *image = [UIImage imageNamed:imageName
- inBundle:[NSBundle mgl_frameworkBundle]
- compatibleWithTraitCollection:nil];
-
- if ( ! image)
- {
- [NSException raise:MGLResourceNotFoundException format:
- @"The resource named “%@” could not be found in the Mapbox framework bundle.", imageName];
- }
-
- return image;
-}
-
- (BOOL)isFullyLoaded
{
return self.mbglMap.isFullyLoaded();
diff --git a/platform/ios/src/MGLMapView_Private.h b/platform/ios/src/MGLMapView_Private.h
index 5aa4902a91..e53dc8519c 100644
--- a/platform/ios/src/MGLMapView_Private.h
+++ b/platform/ios/src/MGLMapView_Private.h
@@ -12,6 +12,9 @@ namespace mbgl {
class MGLMapViewImpl;
@class MGLSource;
+/// Standard animation duration for UI elements.
+FOUNDATION_EXTERN const NSTimeInterval MGLAnimationDuration;
+
/// Minimum size of an annotation’s accessibility element.
FOUNDATION_EXTERN const CGSize MGLAnnotationAccessibilityElementMinimumSize;
diff --git a/platform/ios/src/Mapbox.h b/platform/ios/src/Mapbox.h
index 635bda490f..6e4e2c99fc 100644
--- a/platform/ios/src/Mapbox.h
+++ b/platform/ios/src/Mapbox.h
@@ -15,7 +15,7 @@ FOUNDATION_EXPORT MGL_EXPORT const unsigned char MapboxVersionString[];
#import "MGLCalloutView.h"
#import "MGLClockDirectionFormatter.h"
#import "MGLCluster.h"
-#import "MGLCompassDirectionFormatter.h"
+#import "MGLCompassButton.h"
#import "MGLCoordinateFormatter.h"
#import "MGLDistanceFormatter.h"
#import "MGLFeature.h"
diff --git a/platform/ios/src/UIImage+MGLAdditions.h b/platform/ios/src/UIImage+MGLAdditions.h
index 22bb740242..55835c50d6 100644
--- a/platform/ios/src/UIImage+MGLAdditions.h
+++ b/platform/ios/src/UIImage+MGLAdditions.h
@@ -1,9 +1,13 @@
#import <UIKit/UIKit.h>
+#import <Mapbox/MGLTypes.h>
+
#include <mbgl/style/image.hpp>
NS_ASSUME_NONNULL_BEGIN
+FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const MGLResourceNotFoundException;
+
@interface UIImage (MGLAdditions)
- (nullable instancetype)initWithMGLStyleImage:(const mbgl::style::Image *)styleImage;
@@ -14,6 +18,8 @@ NS_ASSUME_NONNULL_BEGIN
- (mbgl::PremultipliedImage)mgl_premultipliedImage;
++ (UIImage *)mgl_resourceImageNamed:(NSString *)imageName;
+
@end
NS_ASSUME_NONNULL_END
diff --git a/platform/ios/src/UIImage+MGLAdditions.mm b/platform/ios/src/UIImage+MGLAdditions.mm
index 884f92e003..9d05abd6ca 100644
--- a/platform/ios/src/UIImage+MGLAdditions.mm
+++ b/platform/ios/src/UIImage+MGLAdditions.mm
@@ -1,7 +1,10 @@
#import "UIImage+MGLAdditions.h"
+#import "NSBundle+MGLAdditions.h"
#include <mbgl/util/image+MGLAdditions.hpp>
+const MGLExceptionName MGLResourceNotFoundException = @"MGLResourceNotFoundException";
+
@implementation UIImage (MGLAdditions)
- (nullable instancetype)initWithMGLStyleImage:(const mbgl::style::Image *)styleImage
@@ -42,7 +45,20 @@
float(self.scale), isTemplate);
}
--(mbgl::PremultipliedImage)mgl_premultipliedImage {
+- (mbgl::PremultipliedImage)mgl_premultipliedImage {
return MGLPremultipliedImageFromCGImage(self.CGImage);
}
+
++ (UIImage *)mgl_resourceImageNamed:(NSString *)imageName {
+ UIImage *image = [UIImage imageNamed:imageName
+ inBundle:[NSBundle mgl_frameworkBundle]
+ compatibleWithTraitCollection:nil];
+
+ if (!image) {
+ [NSException raise:MGLResourceNotFoundException format:@"The resource named “%@” could not be found in the Mapbox framework bundle.", imageName];
+ }
+
+ return image;
+}
+
@end
diff --git a/platform/ios/test/MGLCompassViewTests.m b/platform/ios/test/MGLCompassViewTests.m
new file mode 100644
index 0000000000..cabd6085a7
--- /dev/null
+++ b/platform/ios/test/MGLCompassViewTests.m
@@ -0,0 +1,23 @@
+#import <Mapbox/Mapbox.h>
+#import <XCTest/XCTest.h>
+
+@interface MGLCompassViewTests : XCTestCase
+
+@end
+
+@implementation MGLCompassViewTests
+
+- (void)setUp {
+ // Put setup code here. This method is called before the invocation of each test method in the class.
+}
+
+- (void)tearDown {
+ // Put teardown code here. This method is called after the invocation of each test method in the class.
+}
+
+- (void)testExample {
+ // This is an example of a functional test case.
+ // Use XCTAssert and related functions to verify your tests produce the correct results.
+}
+
+@end