Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User puck rotates according to heading for walking profile #4129

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions Sources/MapboxCoreNavigation/NavigationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,26 @@ extension MapboxNavigationService: CLLocationManagerDelegate {
// MARK: Handling LocationManager Output

public func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
// Check if device orientation has changed and inform the location provider accordingly.
updateHeadingForCurrentDeviceOrientation()
jill-cardamon marked this conversation as resolved.
Show resolved Hide resolved
router.locationManager?(manager, didUpdateHeading: newHeading)
}

public func updateHeadingForCurrentDeviceOrientation() {
var orientation: CLDeviceOrientation
switch UIDevice.current.orientation {
case .landscapeLeft:
orientation = .landscapeRight
case .landscapeRight:
orientation = .landscapeLeft
default:
orientation = .portrait
}
if locationManager.headingOrientation != orientation {
locationManager.headingOrientation = orientation
}
}

public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//If we're always simulating, make sure this is a simulated update.
if simulationMode == .always, manager != simulatedLocationSource { return }
Expand Down
37 changes: 35 additions & 2 deletions Sources/MapboxNavigation/NavigationMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ open class NavigationMapView: UIView {
public var showsCongestionForAlternativeRoutes: Bool = false

/**
Controls wheter to show restricted portions of a route line.
Controls whether to show restricted portions of a route line.

Restricted areas are drawn using `routeRestrictedAreaColor` which is customizable.
*/
Expand Down Expand Up @@ -96,6 +96,11 @@ open class NavigationMapView: UIView {
}
}

/**
Controls whether to use a dotted line for the route line when the transport type is walking.
*/
public var usesDottedLineForWalking: Bool = true

/**
Location manager that is used to track accuracy and status authorization changes.
*/
Expand Down Expand Up @@ -165,6 +170,12 @@ open class NavigationMapView: UIView {
}
}

var isWalking: Bool {
get {
return routes?.first?.legs.first?.profileIdentifier == .walking ? true : false
}
}

func updateRouteLineWithRouteLineTracksTraversal() {
if let routes = self.routes {
let offset = fractionTraveled
Expand Down Expand Up @@ -349,6 +360,15 @@ open class NavigationMapView: UIView {
mapView.mapboxMap.style.removeLayers(layerIdentifiers)
mapView.mapboxMap.style.removeSources(sourceIdentifiers)

do {
if mapView.mapboxMap.style.image(withId: NavigationMapView.ImageIdentifier.minusImage) != nil {
try mapView.mapboxMap.style.removeImage(withId: NavigationMapView.ImageIdentifier.minusImage)
}
} catch {
Log.error("Failed to remove image \(NavigationMapView.ImageIdentifier.minusImage) from style with error: \(error.localizedDescription).",
category: .navigationUI)
}

routes = nil
removeLineGradientStops()
}
Expand Down Expand Up @@ -623,13 +643,22 @@ open class NavigationMapView: UIView {
below parentLayerIndentifier: String? = nil,
isMainRoute: Bool = true,
legIndex: Int? = nil) -> String? {
guard let defaultShape = route.shape else { return nil }
guard let defaultShape = route.shape,
let circleImage = Bundle.mapboxNavigation.image(named: "minus")?.withRenderingMode(.alwaysTemplate) else { return nil }
let shape = delegate?.navigationMapView(self, shapeFor: route) ?? defaultShape

let geoJSONSource = self.geoJSONSource(shape)
let sourceIdentifier = route.identifier(.source(isMainRoute: isMainRoute, isSourceCasing: false))

do {
if isWalking && usesDottedLineForWalking && mapView.mapboxMap.style.image(withId: NavigationMapView.ImageIdentifier.minusImage) == nil {
try mapView.mapboxMap.style.addImage(circleImage,
id: NavigationMapView.ImageIdentifier.minusImage,
sdf: true,
stretchX: [ImageStretches(first: 0, second: 0)],
stretchY: [ImageStretches(first: 0, second: 0)])
}

if mapView.mapboxMap.style.sourceExists(withId: sourceIdentifier) {
try mapView.mapboxMap.style.updateGeoJSONSource(withId: sourceIdentifier,
geoJSON: .geometry(.lineString(shape)))
Expand Down Expand Up @@ -659,6 +688,9 @@ open class NavigationMapView: UIView {
lineLayer?.lineWidth = .expression(Expression.routeLineWidthExpression())
lineLayer?.lineJoin = .constant(.round)
lineLayer?.lineCap = .constant(.round)
if isWalking && usesDottedLineForWalking {
lineLayer?.linePattern = .constant(.name(NavigationMapView.ImageIdentifier.minusImage))
}

if isMainRoute {
let congestionFeatures = route.congestionFeatures(legIndex: legIndex, roadClassesWithOverriddenCongestionLevels: roadClassesWithOverriddenCongestionLevels)
Expand Down Expand Up @@ -717,6 +749,7 @@ open class NavigationMapView: UIView {
@discardableResult func addRouteCasingLayer(_ route: Route,
below parentLayerIndentifier: String? = nil,
isMainRoute: Bool = true) -> String? {
if isWalking && usesDottedLineForWalking { return nil }
guard let defaultShape = route.shape else { return nil }
let shape = delegate?.navigationMapView(self, casingShapeFor: route) ?? defaultShape

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ extension NavigationMapView {
static let railroadCrossing = "railroad_crossing"
static let yieldSign = "yield_sign"
static let stopSign = "stop_sign"
static let minusImage = "minus"
}

struct ModelKeyIdentifier {
Expand Down
3 changes: 3 additions & 0 deletions Sources/MapboxNavigation/NavigationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,9 @@ extension NavigationViewController: NavigationServiceDelegate {

let movePuckToCurrentLocation = !(userArrivedAtWaypoint && snapsUserLocationAnnotationToRoute && preventRerouting)
if movePuckToCurrentLocation {
if progress.currentLegProgress.currentStep.transportType == .walking && navigationMapView?.mapView.location.options.puckBearingSource == .course {
navigationMapView?.mapView.location.options.puckBearingSource = .heading
}
navigationMapView?.moveUserLocation(to: location, animated: true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public class NavigationViewportDataSource: ViewportDataSource {
NotificationCenter.default.post(name: .navigationCameraViewportDidChange, object: self, userInfo: [
NavigationCamera.NotificationUserInfoKey.cameraOptions: cameraOptions
])
print("!!! device orientation: \(UIDevice.current.orientation)")
}

// MARK: CameraOptions Methods
Expand Down