Skip to content

Commit 408f851

Browse files
authored
Fix setting initial values for CarPlay navigation camera (#4796)
1 parent b1dd65f commit 408f851

File tree

2 files changed

+122
-69
lines changed

2 files changed

+122
-69
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
* `NavigationRouteOptions.init(waypoints:profileIdentifier:queryItems:)` and `NavigationMatchOptions.init(waypoints:profileIdentifier:queryItems:)` set the default values for `shapeFormat`, `locale`, `distanceMeasurementSystem`, `routeShapeResolution`, `includesSpokenInstructions`, and `includesVisualInstructions` only if the values were not passed in `queryItems`. ([#4794](https://github.com/mapbox/mapbox-navigation-ios/pull/4794))
88

9+
### Map
10+
11+
* Fixed an issue where the initially set CarPlay navigation camera values were being overwritten when updates to this property were disabled.
12+
913
## v2.20.2
1014

1115
### Routing

Sources/MapboxNavigation/NavigationViewportDataSource.swift

Lines changed: 118 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -162,45 +162,57 @@ public class NavigationViewportDataSource: ViewportDataSource {
162162
let followingCameraOptions = options.followingCameraOptions
163163

164164
if let location = rawLocation ?? passiveLocation {
165-
if followingCameraOptions.centerUpdatesAllowed || followingMobileCamera.center == nil {
165+
let centerUpdateAllowed = followingCameraOptions.centerUpdatesAllowed
166+
if centerUpdateAllowed || followingMobileCamera.center == nil {
166167
followingMobileCamera.center = location.coordinate
168+
}
169+
if centerUpdateAllowed || followingCarPlayCamera.center == nil {
167170
followingCarPlayCamera.center = location.coordinate
168171
}
169-
170-
if followingCameraOptions.zoomUpdatesAllowed || followingMobileCamera.zoom == nil {
172+
173+
let zoomUpdatesAllowed = followingCameraOptions.zoomUpdatesAllowed
174+
if zoomUpdatesAllowed || followingMobileCamera.zoom == nil || followingCarPlayCamera.zoom == nil {
171175
let altitude = 4000.0
172176
let zoom = CGFloat(ZoomLevelForAltitude(altitude,
173177
mapView.cameraState.pitch,
174178
location.coordinate.latitude,
175179
mapView.bounds.size))
176-
177-
followingMobileCamera.zoom = zoom
178-
followingCarPlayCamera.zoom = zoom
179-
}
180-
181-
if followingCameraOptions.bearingUpdatesAllowed || followingMobileCamera.bearing == nil {
182-
if followingCameraOptions.followsLocationCourse {
183-
followingMobileCamera.bearing = location.course
184-
followingCarPlayCamera.bearing = location.course
185-
} else {
186-
followingMobileCamera.bearing = 0.0
187-
followingCarPlayCamera.bearing = 0.0
180+
if zoomUpdatesAllowed || followingMobileCamera.zoom == nil {
181+
followingMobileCamera.zoom = zoom
182+
}
183+
if zoomUpdatesAllowed || followingCarPlayCamera.zoom == nil {
184+
followingCarPlayCamera.zoom = zoom
188185
}
189186
}
187+
188+
let bearingUpdatesAllowed = followingCameraOptions.bearingUpdatesAllowed
189+
let bearingValue = followingCameraOptions.followsLocationCourse ? location.course : 0.0
190+
if bearingUpdatesAllowed || followingMobileCamera.bearing == nil {
191+
followingMobileCamera.bearing = bearingValue
192+
}
193+
if bearingUpdatesAllowed || followingCarPlayCamera.bearing == nil {
194+
followingCarPlayCamera.bearing = bearingValue
195+
}
190196

191197
followingMobileCamera.anchor = mapView.center
192198
followingCarPlayCamera.anchor = mapView.center
193-
194-
if followingCameraOptions.pitchUpdatesAllowed || followingMobileCamera.pitch == nil {
199+
200+
let pitchUpdatesAllowed = followingCameraOptions.pitchUpdatesAllowed
201+
if pitchUpdatesAllowed || followingMobileCamera.pitch == nil {
195202
followingMobileCamera.pitch = 0.0
203+
}
204+
if pitchUpdatesAllowed || followingCarPlayCamera.pitch == nil {
196205
followingCarPlayCamera.pitch = 0.0
197206
}
198-
199-
if followingCameraOptions.paddingUpdatesAllowed || followingMobileCamera.padding == nil {
207+
208+
let paddingUpdatesAllowed = followingCameraOptions.paddingUpdatesAllowed
209+
if paddingUpdatesAllowed || followingMobileCamera.padding == nil {
200210
followingMobileCamera.padding = mapView.safeAreaInsets
211+
}
212+
if paddingUpdatesAllowed || followingCarPlayCamera.padding == nil {
201213
followingCarPlayCamera.padding = mapView.safeAreaInsets
202214
}
203-
215+
204216
return
205217
}
206218

@@ -249,8 +261,9 @@ public class NavigationViewportDataSource: ViewportDataSource {
249261

250262
let coordinatesForManeuverFraming = compoundManeuvers.reduce([], +)
251263
let coordinatesToManeuver = routeProgress.currentLegProgress.currentStepProgress.remainingStepCoordinates()
252-
253-
if options.followingCameraOptions.centerUpdatesAllowed || followingMobileCamera.center == nil {
264+
265+
let centerUpdatesAllowed = options.followingCameraOptions.centerUpdatesAllowed
266+
if centerUpdatesAllowed || followingMobileCamera.center == nil || followingCarPlayCamera.center == nil {
254267
var center = location.coordinate
255268
if let boundingBox = BoundingBox(from: coordinatesToManeuver + coordinatesForManeuverFraming) {
256269
let coordinates = [
@@ -265,40 +278,50 @@ public class NavigationViewportDataSource: ViewportDataSource {
265278
center = adjustedCenter
266279
}
267280
}
268-
269-
followingMobileCamera.center = center
270-
followingCarPlayCamera.center = center
281+
282+
if centerUpdatesAllowed || followingMobileCamera.center == nil {
283+
followingMobileCamera.center = center
284+
}
285+
if centerUpdatesAllowed || followingCarPlayCamera.center == nil {
286+
followingCarPlayCamera.center = center
287+
}
271288
}
272289

273290
let lookaheadDistance = self.lookaheadDistance(routeProgress)
274-
275-
if options.followingCameraOptions.zoomUpdatesAllowed || followingMobileCamera.zoom == nil {
291+
292+
let zoomUpdatesAllowed = options.followingCameraOptions.zoomUpdatesAllowed
293+
if zoomUpdatesAllowed || followingMobileCamera.zoom == nil || followingCarPlayCamera.zoom == nil {
276294
let defaultZoomLevel = 12.0
277295

278296
let coordinatesForIntersections = coordinatesToManeuver.sliced(from: nil,
279297
to: LineString(coordinatesToManeuver).coordinateFromStart(distance: lookaheadDistance))
280-
281-
let followingMobileCameraZoom = zoom(coordinatesForIntersections,
282-
pitch: pitch,
283-
maxPitch: followingCameraOptions.defaultPitch,
284-
edgeInsets: viewportPadding,
285-
defaultZoomLevel: defaultZoomLevel,
286-
maxZoomLevel: followingCameraOptions.zoomRange.upperBound,
287-
minZoomLevel: followingCameraOptions.zoomRange.lowerBound)
288-
289-
followingMobileCamera.zoom = followingMobileCameraZoom
290-
291-
let followingCarPlayCameraZoom = zoom(coordinatesForIntersections,
292-
pitch: pitch,
293-
maxPitch: followingCameraOptions.defaultPitch,
294-
edgeInsets: carPlayCameraPadding,
295-
defaultZoomLevel: defaultZoomLevel,
296-
maxZoomLevel: followingCameraOptions.zoomRange.upperBound,
297-
minZoomLevel: followingCameraOptions.zoomRange.lowerBound)
298-
followingCarPlayCamera.zoom = followingCarPlayCameraZoom
298+
299+
if zoomUpdatesAllowed || followingMobileCamera.zoom == nil {
300+
let followingMobileCameraZoom = zoom(coordinatesForIntersections,
301+
pitch: pitch,
302+
maxPitch: followingCameraOptions.defaultPitch,
303+
edgeInsets: viewportPadding,
304+
defaultZoomLevel: defaultZoomLevel,
305+
maxZoomLevel: followingCameraOptions.zoomRange.upperBound,
306+
minZoomLevel: followingCameraOptions.zoomRange.lowerBound)
307+
308+
followingMobileCamera.zoom = followingMobileCameraZoom
309+
}
310+
311+
if zoomUpdatesAllowed || followingCarPlayCamera.zoom == nil {
312+
let followingCarPlayCameraZoom = zoom(coordinatesForIntersections,
313+
pitch: pitch,
314+
maxPitch: followingCameraOptions.defaultPitch,
315+
edgeInsets: carPlayCameraPadding,
316+
defaultZoomLevel: defaultZoomLevel,
317+
maxZoomLevel: followingCameraOptions.zoomRange.upperBound,
318+
minZoomLevel: followingCameraOptions.zoomRange.lowerBound)
319+
followingCarPlayCamera.zoom = followingCarPlayCameraZoom
320+
}
299321
}
300-
301-
if options.followingCameraOptions.bearingUpdatesAllowed || followingMobileCamera.bearing == nil {
322+
323+
let bearingUpdatesAllowed = options.followingCameraOptions.bearingUpdatesAllowed
324+
if bearingUpdatesAllowed || followingMobileCamera.bearing == nil || followingCarPlayCamera.bearing == nil {
302325
var bearing = location.course
303326
let lookaheadDistance = self.lookaheadDistance(routeProgress)
304327
let distance = fmax(lookaheadDistance, geometryFramingAfterManeuver.enabled
@@ -320,9 +343,13 @@ public class NavigationViewportDataSource: ViewportDataSource {
320343
headingDirection = bearing
321344
}
322345
}
323-
324-
followingMobileCamera.bearing = !isWalking ? bearing : headingDirection
325-
followingCarPlayCamera.bearing = bearing
346+
347+
if zoomUpdatesAllowed || followingMobileCamera.bearing == nil {
348+
followingMobileCamera.bearing = !isWalking ? bearing : headingDirection
349+
}
350+
if zoomUpdatesAllowed || followingCarPlayCamera.bearing == nil {
351+
followingCarPlayCamera.bearing = bearing
352+
}
326353
}
327354

328355
let followingMobileCameraAnchor = anchor(pitchСoefficient,
@@ -336,18 +363,23 @@ public class NavigationViewportDataSource: ViewportDataSource {
336363
edgeInsets: carPlayCameraPadding)
337364

338365
followingCarPlayCamera.anchor = followingCarPlayCameraAnchor
339-
340-
if options.followingCameraOptions.pitchUpdatesAllowed || followingMobileCamera.pitch == nil {
366+
367+
let pitchUpdatesAllowed = options.followingCameraOptions.pitchUpdatesAllowed
368+
if pitchUpdatesAllowed || followingMobileCamera.pitch == nil {
341369
followingMobileCamera.pitch = CGFloat(pitch)
370+
}
371+
if pitchUpdatesAllowed || followingCarPlayCamera.pitch == nil {
342372
followingCarPlayCamera.pitch = CGFloat(pitch)
343373
}
344-
345-
if options.followingCameraOptions.paddingUpdatesAllowed || followingMobileCamera.padding == nil {
374+
375+
let paddingUpdatesAllowed = options.followingCameraOptions.paddingUpdatesAllowed
376+
if paddingUpdatesAllowed || followingMobileCamera.padding == nil {
346377
followingMobileCamera.padding = UIEdgeInsets(top: followingMobileCameraAnchor.y,
347378
left: viewportPadding.left,
348379
bottom: mapView.bounds.height - followingMobileCameraAnchor.y + 1.0,
349380
right: viewportPadding.right)
350-
381+
}
382+
if paddingUpdatesAllowed || followingCarPlayCamera.padding == nil {
351383
if mapView.window?.screen.traitCollection.userInterfaceIdiom == .carPlay {
352384
followingCarPlayCamera.padding = UIEdgeInsets(top: followingCarPlayCameraAnchor.y,
353385
left: carPlayCameraPadding.left,
@@ -379,18 +411,25 @@ public class NavigationViewportDataSource: ViewportDataSource {
379411

380412
if overviewCameraOptions.pitchUpdatesAllowed || overviewMobileCamera.pitch == nil {
381413
overviewMobileCamera.pitch = 0.0
414+
}
415+
if overviewCameraOptions.pitchUpdatesAllowed || overviewMobileCamera.pitch == nil {
382416
overviewCarPlayCamera.pitch = 0.0
383417
}
384-
385-
if overviewCameraOptions.centerUpdatesAllowed || overviewMobileCamera.center == nil {
418+
419+
let centerUpdatesAllowed = overviewCameraOptions.centerUpdatesAllowed
420+
if centerUpdatesAllowed || overviewMobileCamera.center == nil || overviewCarPlayCamera.center == nil {
386421
if let boundingBox = BoundingBox(from: remainingCoordinatesOnRoute) {
387422
let center = [
388423
boundingBox.southWest,
389424
boundingBox.northEast
390425
].centerCoordinate
391-
392-
overviewMobileCamera.center = center
393-
overviewCarPlayCamera.center = center
426+
427+
if centerUpdatesAllowed || overviewMobileCamera.center == nil {
428+
overviewMobileCamera.center = center
429+
}
430+
if centerUpdatesAllowed || overviewCarPlayCamera.center == nil {
431+
overviewCarPlayCamera.center = center
432+
}
394433
}
395434
}
396435

@@ -399,8 +438,9 @@ public class NavigationViewportDataSource: ViewportDataSource {
399438

400439
overviewCarPlayCamera.anchor = anchor(bounds: mapView.bounds,
401440
edgeInsets: carPlayCameraPadding)
402-
403-
if overviewCameraOptions.bearingUpdatesAllowed || overviewMobileCamera.bearing == nil {
441+
442+
let bearingUpdatesAllowed = overviewCameraOptions.bearingUpdatesAllowed
443+
if bearingUpdatesAllowed || overviewMobileCamera.bearing == nil || overviewCarPlayCamera.bearing == nil {
404444
// In case if `NavigationCamera` is already in `NavigationCameraState.overview` value
405445
// of bearing will be also ignored.
406446
let bearing = 0.0
@@ -416,26 +456,35 @@ public class NavigationViewportDataSource: ViewportDataSource {
416456
headingDirection = bearing
417457
}
418458
}
419-
420-
overviewMobileCamera.bearing = !isWalking ? bearing : headingDirection
421-
overviewCarPlayCamera.bearing = bearing
459+
if bearingUpdatesAllowed || overviewMobileCamera.bearing == nil {
460+
overviewMobileCamera.bearing = !isWalking ? bearing : headingDirection
461+
}
462+
if bearingUpdatesAllowed || overviewCarPlayCamera.bearing == nil {
463+
overviewCarPlayCamera.bearing = bearing
464+
}
422465
}
423466

424-
if overviewCameraOptions.zoomUpdatesAllowed || overviewMobileCamera.zoom == nil {
467+
let zoomUpdatesAllowed = overviewCameraOptions.zoomUpdatesAllowed
468+
if zoomUpdatesAllowed || overviewMobileCamera.zoom == nil {
425469
overviewMobileCamera.zoom = overviewCameraZoom(remainingCoordinatesOnRoute,
426470
pitch: overviewMobileCamera.pitch,
427471
bearing: overviewMobileCamera.bearing,
428472
edgeInsets: viewportPadding,
429473
maxZoomLevel: overviewCameraOptions.maximumZoomLevel)
474+
}
475+
if zoomUpdatesAllowed || overviewCarPlayCamera.zoom == nil {
430476
overviewCarPlayCamera.zoom = overviewCameraZoom(remainingCoordinatesOnRoute,
431477
pitch: overviewCarPlayCamera.pitch,
432478
bearing: overviewCarPlayCamera.bearing,
433479
edgeInsets: carPlayCameraPadding,
434480
maxZoomLevel: overviewCameraOptions.maximumZoomLevel)
435481
}
436-
437-
if overviewCameraOptions.paddingUpdatesAllowed || overviewMobileCamera.padding == nil {
482+
483+
let paddingUpdatesAllowed = overviewCameraOptions.paddingUpdatesAllowed
484+
if paddingUpdatesAllowed || overviewMobileCamera.padding == nil {
438485
overviewMobileCamera.padding = viewportPadding
486+
}
487+
if paddingUpdatesAllowed || overviewCarPlayCamera.padding == nil {
439488
overviewCarPlayCamera.padding = carPlayCameraPadding
440489
}
441490
}

0 commit comments

Comments
 (0)