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

Fix rotation edge cases and compass overspinning #1829

Merged
merged 4 commits into from
Jul 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/mbgl/util/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ T clamp(T value, T min, T max) {
return value < min ? min : (value > max ? max : value);
}

// Constrains n to the given range (including min, excluding max) via modular
// arithmetic.
template <typename T>
T wrap(T value, T min, T max) {
T d = max - min;
return value == max ? value : std::fmod((std::fmod((value - min), d) + d), d) + min;
return std::fmod((std::fmod((value - min), d) + d), d) + min;
}

template <typename T>
Expand Down
27 changes: 4 additions & 23 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/util/geo.hpp>
#include <mbgl/util/math.hpp>
#include <mbgl/util/constants.hpp>

#import "MapboxGL.h"
Expand Down Expand Up @@ -1399,19 +1400,6 @@ - (void)resetNorthAnimated:(BOOL)animated
CGFloat duration = (animated ? MGLAnimationDuration : 0);

_mbglMap->setBearing(0, secondsAsDuration(duration));

[UIView animateWithDuration:duration
animations:^
{
self.compassView.transform = CGAffineTransformIdentity;
}
completion:^(BOOL finished)
{
if (finished)
{
[self notifyMapChange:(animated ? mbgl::MapChangeRegionDidChangeAnimated : mbgl::MapChangeRegionDidChange)];
}
}];
}

- (void)resetPosition
Expand Down Expand Up @@ -1568,12 +1556,7 @@ - (void)setVisibleCoordinates:(CLLocationCoordinate2D *)coordinates count:(NSUIn

- (CLLocationDirection)direction
{
double direction = _mbglMap->getBearing();

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

return direction;
return mbgl::util::wrap(_mbglMap->getBearing(), 0., 360.);
}

- (void)setDirection:(CLLocationDirection)direction animated:(BOOL)animated
Expand Down Expand Up @@ -2472,7 +2455,6 @@ - (void)notifyMapChange:(mbgl::MapChange)change
case mbgl::MapChangeRegionWillChangeAnimated:
{
[self updateUserLocationAnnotationView];
[self updateCompass];

[self deselectAnnotation:self.selectedAnnotation animated:NO];

Expand Down Expand Up @@ -2515,6 +2497,7 @@ - (void)notifyMapChange:(mbgl::MapChange)change
{
[self.delegate mapViewRegionIsChanging:self];
}
break;
}
case mbgl::MapChangeRegionDidChange:
case mbgl::MapChangeRegionDidChangeAnimated:
Expand Down Expand Up @@ -2624,9 +2607,7 @@ - (void)updateUserLocationAnnotationView

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

self.compassView.transform = CGAffineTransformMakeRotation(MGLRadiansFromDegrees(degrees));

Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/map/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ using namespace mbgl;
static double _normalizeAngle(double angle, double anchorAngle)
{
angle = util::wrap(angle, -M_PI, M_PI);
if (angle == -M_PI) angle = M_PI;
double diff = std::abs(angle - anchorAngle);
if (std::abs(angle - util::M2PI - anchorAngle) < diff) {
angle -= util::M2PI;
Expand Down Expand Up @@ -336,7 +337,7 @@ void Transform::_setAngle(double new_angle, const Duration duration) {

startTransition(
[=](double t) {
state.angle = util::interpolate(startA, angle, t);
state.angle = util::wrap(util::interpolate(startA, angle, t), -M_PI, M_PI);
view.notifyMapChange(MapChangeRegionIsChanging);
return Update::Nothing;
},
Expand Down