Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Made compass accessible
Browse files Browse the repository at this point in the history
Turned the compass into a button. Added accessibility values and hints. The compass’s accessibility value indicates the direction according to the 32-point system. Turned off direct interaction with the map because it obscures the ornaments.
  • Loading branch information
1ec5 committed May 12, 2015
1 parent 08a7ca9 commit 90af18e
Showing 1 changed file with 59 additions and 13 deletions.
72 changes: 59 additions & 13 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,48 @@
return [NSURL URLWithString:[NSString stringWithFormat:@"asset://styles/%@.json", styleName]];
}

// Boxes the compass
static NSString *MGLDescriptionForDirection(CLLocationDirection direction)
{
NSArray *descriptions = @[
@"North",
@"North by east",
@"North-northeast",
@"Northeast by north",
@"Northeast",
@"Northeast by east",
@"East-northeast",
@"East by north",
@"East",
@"East by south",
@"East-southeast",
@"Southeast by east",
@"Southeast",
@"Southeast by south",
@"South-southeast",
@"South by east",
@"South",
@"South by west",
@"South-southwest",
@"Southwest by south",
@"Southwest",
@"Southwest by west",
@"West-southwest",
@"West by south",
@"West",
@"West by north",
@"West-northwest",
@"Northwest by west",
@"Northwest",
@"Northwest by north",
@"North-northwest",
@"North by west",
];

NSUInteger cardinalPoint = round(direction / 360 * 32);
return descriptions[cardinalPoint];
}

#pragma mark - Private -

@interface MGLMapView () <UIGestureRecognizerDelegate, GLKViewDelegate, CLLocationManagerDelegate, UIActionSheetDelegate>
Expand All @@ -60,7 +102,7 @@ @interface MGLMapView () <UIGestureRecognizerDelegate, GLKViewDelegate, CLLocati
@property (nonatomic) GLKView *glView;
@property (nonatomic) UIImageView *glSnapshotView;
@property (nonatomic) NSOperationQueue *regionChangeDelegateQueue;
@property (nonatomic) UIImageView *compass;
@property (nonatomic) UIButton *compass;
@property (nonatomic) UIImageView *logoBug;
@property (nonatomic) UIButton *attributionButton;
@property (nonatomic) UIActionSheet *attributionSheet;
Expand Down Expand Up @@ -209,7 +251,7 @@ - (BOOL)commonInit

// setup accessibility
//
self.isAccessibilityElement = YES;
// self.isAccessibilityElement = YES;
self.accessibilityLabel = @"Map";
self.accessibilityLanguage = @"en";
self.accessibilityTraits = UIAccessibilityTraitAllowsDirectInteraction;
Expand Down Expand Up @@ -309,7 +351,8 @@ - (BOOL)commonInit
// setup attribution
//
_attributionButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
_attributionButton.accessibilityLabel = @"Attribution info";
_attributionButton.accessibilityLabel = @"Credits";
_attributionButton.accessibilityHint = @"Opens a webpage with credits and a feedback form";
[_attributionButton addTarget:self action:@selector(showAttribution) forControlEvents:UIControlEventTouchUpInside];
_attributionButton.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_attributionButton];
Expand All @@ -326,15 +369,17 @@ - (BOOL)commonInit

// setup compass
//
_compass = [[UIImageView alloc] initWithImage:[MGLMapView resourceImageNamed:@"Compass.png"]];
_compass.accessibilityLabel = @"Compass";
_compass = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *compassImage = [MGLMapView resourceImageNamed:@"Compass.png"];
[_compass setImage:compassImage forState:UIControlStateNormal];
_compass.frame = CGRectMake(0, 0, compassImage.size.width, compassImage.size.height);
_compass.alpha = 0;
_compass.accessibilityLabel = @"Compass";
_compass.accessibilityHint = @"Rotates the map to face due north";
UIView *container = [[UIView alloc] initWithFrame:CGRectZero];
[container addSubview:_compass];
container.translatesAutoresizingMaskIntoConstraints = NO;
[container addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleCompassTapGesture:)]];
[_compass addTarget:self action:@selector(resetNorthWithCompass:) forControlEvents:UIControlEventTouchDown];
[self addSubview:container];

// setup interaction
Expand Down Expand Up @@ -547,14 +592,15 @@ - (void)updateConstraints
multiplier:1
constant:5]];

UIImage *compassImage = [self.compass imageForState:UIControlStateNormal];
[compassContainer addConstraint:
[NSLayoutConstraint constraintWithItem:compassContainer
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:self.compass.image.size.width]];
constant:compassImage.size.width]];

[compassContainer addConstraint:
[NSLayoutConstraint constraintWithItem:compassContainer
Expand All @@ -563,7 +609,7 @@ - (void)updateConstraints
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:self.compass.image.size.height]];
constant:compassImage.size.height]];

// logo bug
//
Expand Down Expand Up @@ -742,7 +788,7 @@ - (void)updateTintColorForView:(UIView *)view

#pragma mark - Gestures -

- (void)handleCompassTapGesture:(id)sender
- (void)resetNorthWithCompass:(id)sender
{
[self resetNorthAnimated:YES];

Expand Down Expand Up @@ -1345,6 +1391,7 @@ - (void)resetNorthAnimated:(BOOL)animated
animations:^
{
self.compass.transform = CGAffineTransformIdentity;
self.compass.accessibilityValue = MGLDescriptionForDirection(0);
}
completion:^(BOOL finished)
{
Expand Down Expand Up @@ -1512,7 +1559,7 @@ - (CLLocationDirection)direction
{
double direction = _mbglMap->getBearing() * -1;

while (direction > 360) direction -= 360;
while (direction >= 360) direction -= 360;
while (direction < 0) direction += 360;

return direction;
Expand Down Expand Up @@ -2479,11 +2526,10 @@ - (void)updateUserLocationAnnotationView

- (void)updateCompass
{
double degrees = _mbglMap->getBearing() * -1;
while (degrees >= 360) degrees -= 360;
while (degrees < 0) degrees += 360;
CLLocationDirection degrees = self.direction;

self.compass.transform = CGAffineTransformMakeRotation([MGLMapView degreesToRadians:degrees]);
self.compass.accessibilityValue = MGLDescriptionForDirection(degrees);

if (_mbglMap->getBearing() && self.compass.alpha < 1)
{
Expand Down

0 comments on commit 90af18e

Please sign in to comment.