Skip to content

Commit

Permalink
[Cherry pick] Use camera options to calculate zoom for overview camera (
Browse files Browse the repository at this point in the history
#4600)

* Use camera options to calculate zoom for overview camera
  • Loading branch information
kried authored Feb 16, 2024
1 parent b91e86e commit 1e38063
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
### Map

* Fixed a possible crash that could happen when displaying the route with the same source, midpoint, and destination. ([#4576](https://github.com/mapbox/mapbox-navigation-ios/pull/4576))
* Fixed an incorrect viewport padding in the overview route camera. ([#4593](https://github.com/mapbox/mapbox-navigation-ios/pull/4593))

### User interface

Expand Down
49 changes: 36 additions & 13 deletions Sources/MapboxNavigation/NavigationViewportDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,11 @@ public class NavigationViewportDataSource: ViewportDataSource {
.map({ $0.shape?.coordinates })
let untraveledCoordinatesOnCurrentStep = routeProgress.currentLegProgress.currentStepProgress.remainingStepCoordinates()
let remainingCoordinatesOnRoute = coordinatesAfterCurrentStep.flatten() + untraveledCoordinatesOnCurrentStep
let carPlayCameraPadding = mapView.safeArea + UIEdgeInsets.centerEdgeInsets

var carPlayCameraPadding = mapView.safeArea + UIEdgeInsets.centerEdgeInsets
// NOTE: We need this extra padding in CarPlay to avoid overlap of the route, street name labels, and control buttons.
carPlayCameraPadding.top += 20 // destination pin
carPlayCameraPadding.bottom += 38.0 // way name view
let overviewCameraOptions = options.overviewCameraOptions

if overviewCameraOptions.pitchUpdatesAllowed || overviewMobileCamera.pitch == nil {
Expand All @@ -389,17 +393,7 @@ public class NavigationViewportDataSource: ViewportDataSource {
overviewCarPlayCamera.center = center
}
}

if overviewCameraOptions.zoomUpdatesAllowed || overviewMobileCamera.zoom == nil {
overviewMobileCamera.zoom = zoom(remainingCoordinatesOnRoute,
edgeInsets: viewportPadding,
maxZoomLevel: overviewCameraOptions.maximumZoomLevel)

overviewCarPlayCamera.zoom = zoom(remainingCoordinatesOnRoute,
edgeInsets: carPlayCameraPadding,
maxZoomLevel: overviewCameraOptions.maximumZoomLevel)
}


overviewMobileCamera.anchor = anchor(bounds: mapView.bounds,
edgeInsets: viewportPadding)

Expand All @@ -426,6 +420,19 @@ public class NavigationViewportDataSource: ViewportDataSource {
overviewMobileCamera.bearing = !isWalking ? bearing : headingDirection
overviewCarPlayCamera.bearing = bearing
}

if overviewCameraOptions.zoomUpdatesAllowed || overviewMobileCamera.zoom == nil {
overviewMobileCamera.zoom = overviewCameraZoom(remainingCoordinatesOnRoute,
pitch: overviewMobileCamera.pitch,
bearing: overviewMobileCamera.bearing,
edgeInsets: viewportPadding,
maxZoomLevel: overviewCameraOptions.maximumZoomLevel)
overviewCarPlayCamera.zoom = overviewCameraZoom(remainingCoordinatesOnRoute,
pitch: overviewCarPlayCamera.pitch,
bearing: overviewCarPlayCamera.bearing,
edgeInsets: carPlayCameraPadding,
maxZoomLevel: overviewCameraOptions.maximumZoomLevel)
}

if overviewCameraOptions.paddingUpdatesAllowed || overviewMobileCamera.padding == nil {
overviewMobileCamera.padding = viewportPadding
Expand Down Expand Up @@ -454,7 +461,23 @@ public class NavigationViewportDataSource: ViewportDataSource {
let mapViewBearing = Double(mapView?.cameraState.bearing ?? 0.0)
return mapViewBearing + bearing.shortestRotation(angle: mapViewBearing)
}


func overviewCameraZoom(_ coordinates: [CLLocationCoordinate2D],
pitch: CGFloat?,
bearing: CLLocationDirection?,
edgeInsets: UIEdgeInsets,
defaultZoomLevel: Double = 12.0,
maxZoomLevel: Double = 22.0,
minZoomLevel: Double = 2.0) -> CGFloat {
guard let mapView = mapView else { return CGFloat(defaultZoomLevel) }

let options = mapView.mapboxMap.camera(for: coordinates,
padding: edgeInsets,
bearing: 0,
pitch: 0)
return CGFloat(max(min(options.zoom ?? defaultZoomLevel, maxZoomLevel), minZoomLevel))
}

func zoom(_ coordinates: [CLLocationCoordinate2D],
pitch: Double = 0.0,
maxPitch: Double = 0.0,
Expand Down

0 comments on commit 1e38063

Please sign in to comment.