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

Commit

Permalink
[ios] Make annotation view rotation alignment configurable (#9147)
Browse files Browse the repository at this point in the history
This commit adds `rotatesWithMap` property on `MGLAnnotationView`. This
property, when set to `YES` fixes the annotation to a map such that view
follows map's rotation angle. This is useful when user wants to display
rotation-dependent annotations (e.g. sector lights).
  • Loading branch information
boundsj authored Jun 1, 2017
1 parent b2f277a commit 012e88c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT
### Annotations

* Added a new initializer to `MGLAnnotationView` so that it is possible to create a new instance with an associated annotation object. ([#9029](https://github.com/mapbox/mapbox-gl-native/pull/9029))
* Added a new `rotatesToMatchCamera` property to `MGLAnnotationView` that, when set to true, causes the annotation view to rotate along with the map's rotation angle giving the appearance that the annoation view is pinned to the map. ([#9147](https://github.com/mapbox/mapbox-gl-native/pull/9147))
* Fixed an issue causing a view-backed annotation to disappear immediately instead of animating when the annotation’s `coordinate` property is set to a value outside the current viewport. ([#8565](https://github.com/mapbox/mapbox-gl-native/pull/8565))
* Fixed an issue in which `MGLMapView` overrode the tint colors of its annotation views. ([#8789](https://github.com/mapbox/mapbox-gl-native/pull/8789))
* Fixed an issue causing annotation views to persist in the map’s annotation container view even after their associated annotations were removed. ([#9025](https://github.com/mapbox/mapbox-gl-native/pull/9025))
Expand Down
13 changes: 13 additions & 0 deletions platform/ios/src/MGLAnnotationView.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ typedef NS_ENUM(NSUInteger, MGLAnnotationViewDragState) {
*/
@property (nonatomic, assign) BOOL scalesWithViewingDistance;

/**
A Boolean value that determines whether the annotation view rotates together
with the map.
When the value of this property is `YES` and the map is rotated, the annotation
view rotates. This is also the behavior of `MGLAnnotationImage` objects. When the
value of this property is `NO` the annotation has its rotation angle fixed.
The default value of this property is `NO`. Set this property to `YES` if the
view’s rotation is important.
*/
@property (nonatomic, assign) BOOL rotatesToMatchCamera;

#pragma mark Managing the Selection State

/**
Expand Down
24 changes: 24 additions & 0 deletions platform/ios/src/MGLAnnotationView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ @interface MGLAnnotationView () <UIGestureRecognizerDelegate>

@property (nonatomic, readwrite, nullable) NSString *reuseIdentifier;
@property (nonatomic, readwrite) CATransform3D lastAppliedScaleTransform;
@property (nonatomic, readwrite) CATransform3D lastAppliedRotateTransform;
@property (nonatomic, weak) UIPanGestureRecognizer *panGestureRecognizer;
@property (nonatomic, weak) UILongPressGestureRecognizer *longPressRecognizer;
@property (nonatomic, weak) MGLMapView *mapView;
Expand Down Expand Up @@ -53,6 +54,7 @@ - (instancetype)initWithCoder:(NSCoder *)decoder {
_annotation = [decoder decodeObjectOfClass:[NSObject class] forKey:@"annotation"];
_centerOffset = [decoder decodeCGVectorForKey:@"centerOffset"];
_scalesWithViewingDistance = [decoder decodeBoolForKey:@"scalesWithViewingDistance"];
_rotatesToMatchCamera = [decoder decodeBoolForKey:@"rotatesToMatchCamera"];
_selected = [decoder decodeBoolForKey:@"selected"];
_enabled = [decoder decodeBoolForKey:@"enabled"];
self.draggable = [decoder decodeBoolForKey:@"draggable"];
Expand All @@ -66,6 +68,7 @@ - (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:_annotation forKey:@"annotation"];
[coder encodeCGVector:_centerOffset forKey:@"centerOffset"];
[coder encodeBool:_scalesWithViewingDistance forKey:@"scalesWithViewingDistance"];
[coder encodeBool:_rotatesToMatchCamera forKey:@"rotatesToMatchCamera"];
[coder encodeBool:_selected forKey:@"selected"];
[coder encodeBool:_enabled forKey:@"enabled"];
[coder encodeBool:_draggable forKey:@"draggable"];
Expand Down Expand Up @@ -109,6 +112,7 @@ - (void)setCenter:(CGPoint)center

super.center = center;
[self updateScaleTransformForViewingDistance];
[self updateRotateTransform];
}

- (void)setScalesWithViewingDistance:(BOOL)scalesWithViewingDistance
Expand Down Expand Up @@ -157,6 +161,26 @@ - (void)updateScaleTransformForViewingDistance
}
}

- (void)setRotatesToMatchCamera:(BOOL)rotatesToMatchCamera
{
if (_rotatesToMatchCamera != rotatesToMatchCamera)
{
_rotatesToMatchCamera = rotatesToMatchCamera;
[self updateRotateTransform];
}
}

- (void)updateRotateTransform
{
if (self.rotatesToMatchCamera == NO) return;

CGFloat directionRad = self.mapView.direction * M_PI / 180.0;
CATransform3D newRotateTransform = CATransform3DMakeRotation(-directionRad, 0, 0, 1);
self.layer.transform = CATransform3DConcat(CATransform3DIdentity, newRotateTransform);

_lastAppliedRotateTransform = newRotateTransform;
}

#pragma mark - Draggable

- (void)setDraggable:(BOOL)draggable
Expand Down

0 comments on commit 012e88c

Please sign in to comment.