Skip to content

Commit

Permalink
MGLUserTrackingModeFollowWithCourse
Browse files Browse the repository at this point in the history
Added course-tracking. However, rotation has to happen atomically without animation until mapbox#1834 is fixed.

Fixes mapbox#1605.
  • Loading branch information
1ec5 committed Aug 14, 2015
1 parent 4904957 commit 33119c0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion include/mbgl/ios/MGLMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ IB_DESIGNABLE

/** Tells the delegate that the location of the user was updated.
*
* While the showsUserLocation property is set to `YES`, this method is called whenever a new location update is received by the map view. This method is also called if the map view’s user tracking mode is set to MGLUserTrackingModeFollowWithHeading and the heading changes.
* While the showsUserLocation property is set to `YES`, this method is called whenever a new location update is received by the map view. This method is also called if the map view’s user tracking mode is set to MGLUserTrackingModeFollowWithHeading and the heading changes, or if it is set to MGLUserTrackingModeFollowWithCourse and the course changes.
*
* This method is not called if the application is currently running in the background. If you want to receive location updates while running in the background, you must use the Core Location framework.
*
Expand Down
4 changes: 3 additions & 1 deletion include/mbgl/ios/MGLTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ typedef NS_ENUM(NSUInteger, MGLUserTrackingMode) {
/** The map follows the user location. */
MGLUserTrackingModeFollow,
/** The map follows the user location and rotates when the heading changes. */
MGLUserTrackingModeFollowWithHeading
MGLUserTrackingModeFollowWithHeading,
/** The map follows the user location and rotates when the course changes. */
MGLUserTrackingModeFollowWithCourse,
};

NS_ASSUME_NONNULL_END
Expand Down
25 changes: 14 additions & 11 deletions ios/app/MBXViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -310,18 +310,20 @@ - (void)cycleStyles

- (void)locateUser
{
if (self.mapView.userTrackingMode == MGLUserTrackingModeNone)
{
self.mapView.userTrackingMode = MGLUserTrackingModeFollow;
}
else if (self.mapView.userTrackingMode == MGLUserTrackingModeFollow)
{
self.mapView.userTrackingMode = MGLUserTrackingModeFollowWithHeading;
}
else
{
self.mapView.userTrackingMode = MGLUserTrackingModeNone;
MGLUserTrackingMode nextMode;
switch (self.mapView.userTrackingMode) {
case MGLUserTrackingModeNone:
nextMode = MGLUserTrackingModeFollow;
break;
case MGLUserTrackingModeFollow:
nextMode = MGLUserTrackingModeFollowWithHeading;
break;
case MGLUserTrackingModeFollowWithHeading:
case MGLUserTrackingModeFollowWithCourse:
nextMode = MGLUserTrackingModeNone;
break;
}
self.mapView.userTrackingMode = nextMode;
}

#pragma mark - Destruction
Expand Down Expand Up @@ -413,6 +415,7 @@ - (void)mapView:(__unused MGLMapView *)mapView didChangeUserTrackingMode:(MGLUse
break;

case MGLUserTrackingModeFollowWithHeading:
case MGLUserTrackingModeFollowWithCourse:
newButtonImage = [UIImage imageNamed:@"TrackingHeadingMask.png"];
break;
}
Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/settings_nsuserdefaults.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

unsigned uncheckedTrackingMode = [settings[@"trackingMode"] unsignedIntValue];
if (uncheckedTrackingMode > MGLUserTrackingModeNone &&
uncheckedTrackingMode <= MGLUserTrackingModeFollowWithHeading)
uncheckedTrackingMode <= MGLUserTrackingModeFollowWithCourse)
{
userTrackingMode = (MGLUserTrackingMode)uncheckedTrackingMode;
}
Expand Down
21 changes: 16 additions & 5 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,11 @@ - (void)handleCompassTapGesture:(__unused id)sender
{
[self resetNorthAnimated:YES];

if (self.userTrackingMode == MGLUserTrackingModeFollowWithHeading) self.userTrackingMode = MGLUserTrackingModeFollow;
if (self.userTrackingMode == MGLUserTrackingModeFollowWithHeading ||
self.userTrackingMode == MGLUserTrackingModeFollowWithCourse)
{
self.userTrackingMode = MGLUserTrackingModeFollow;
}
}

- (void)touchesBegan:(__unused NS_SET_OF(UITouch *) *)touches withEvent:(__unused UIEvent *)event
Expand Down Expand Up @@ -2278,7 +2282,8 @@ - (void)setUserTrackingMode:(MGLUserTrackingMode)mode animated:(BOOL)animated
{
if (mode == _userTrackingMode) return;

if (mode == MGLUserTrackingModeFollowWithHeading && ! CLLocationCoordinate2DIsValid(self.userLocation.coordinate))
if ((mode == MGLUserTrackingModeFollowWithHeading || mode == MGLUserTrackingModeFollowWithCourse) &&
! CLLocationCoordinate2DIsValid(self.userLocation.coordinate))
{
mode = MGLUserTrackingModeNone;
}
Expand All @@ -2288,7 +2293,6 @@ - (void)setUserTrackingMode:(MGLUserTrackingMode)mode animated:(BOOL)animated
switch (_userTrackingMode)
{
case MGLUserTrackingModeNone:
default:
{
[self.locationManager stopUpdatingHeading];

Expand All @@ -2311,6 +2315,7 @@ - (void)setUserTrackingMode:(MGLUserTrackingMode)mode animated:(BOOL)animated
break;
}
case MGLUserTrackingModeFollowWithHeading:
case MGLUserTrackingModeFollowWithCourse:
{
self.showsUserLocation = YES;

Expand Down Expand Up @@ -2404,6 +2409,12 @@ - (void)locationManager:(__unused CLLocationManager *)manager didUpdateToLocatio
}
}

CLLocationDirection course = self.userLocation.location.course;
if (course >= 0 && self.userTrackingMode == MGLUserTrackingModeFollowWithCourse)
{
_mbglMap->setBearing(course);
}

self.userLocationAnnotationView.haloLayer.hidden = ! CLLocationCoordinate2DIsValid(self.userLocation.coordinate) ||
newLocation.horizontalAccuracy > 10;

Expand Down Expand Up @@ -2432,9 +2443,9 @@ - (void)locationManager:(__unused CLLocationManager *)manager didUpdateHeading:(
if ( ! _showsUserLocation) return;
}

CLLocationDirection headingDirection = (newHeading.trueHeading > 0 ? newHeading.trueHeading : newHeading.magneticHeading);
CLLocationDirection headingDirection = (newHeading.trueHeading >= 0 ? newHeading.trueHeading : newHeading.magneticHeading);

if (headingDirection > 0 && self.userTrackingMode == MGLUserTrackingModeFollowWithHeading)
if (headingDirection >= 0 && self.userTrackingMode == MGLUserTrackingModeFollowWithHeading)
{
_mbglMap->setBearing(headingDirection, secondsAsDuration(MGLAnimationDuration));
}
Expand Down
3 changes: 2 additions & 1 deletion platform/ios/MGLUserLocationAnnotationView.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ - (void)setupLayers
//
if (_headingIndicatorLayer)
{
_headingIndicatorLayer.hidden = (_mapView.userTrackingMode == MGLUserTrackingModeFollowWithHeading) ? NO : YES;
_headingIndicatorLayer.hidden = !(_mapView.userTrackingMode == MGLUserTrackingModeFollowWithHeading ||
_mapView.userTrackingMode == MGLUserTrackingModeFollowWithCourse);

if (_oldHeadingAccuracy != self.annotation.heading.headingAccuracy)
{
Expand Down

0 comments on commit 33119c0

Please sign in to comment.