Skip to content

Commit

Permalink
Use camera options to calculate zoom for overview camera
Browse files Browse the repository at this point in the history
  • Loading branch information
kried committed Feb 8, 2024
1 parent 61b4bba commit 2b03933
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 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
44 changes: 32 additions & 12 deletions Sources/MapboxNavigation/NavigationViewportDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -389,17 +389,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 +416,20 @@ 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)

// NOTE: zoom method adds some extra padding to the viewport. We need this extra padding in CarPlay
// to avoid overlap of the route, street name labels, and control buttons.
overviewCarPlayCamera.zoom = zoom(remainingCoordinatesOnRoute,
edgeInsets: carPlayCameraPadding,
maxZoomLevel: overviewCameraOptions.maximumZoomLevel)
}

if overviewCameraOptions.paddingUpdatesAllowed || overviewMobileCamera.padding == nil {
overviewMobileCamera.padding = viewportPadding
Expand Down Expand Up @@ -454,7 +458,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 2b03933

Please sign in to comment.