Skip to content

Commit

Permalink
Use camera options to calculate zoom for overview camera (#4593)
Browse files Browse the repository at this point in the history
* Use camera options to calculate zoom for overview camera
  • Loading branch information
kried authored Feb 9, 2024
1 parent 61b4bba commit bbbfac3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 40 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,92 @@
"images" : [
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x"
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x"
"scale" : "3x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
"scale" : "3x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
"scale" : "3x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "60x60"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "60x60"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "1x"
"scale" : "1x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "2x"
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "1x"
"scale" : "1x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "2x"
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "1x"
"scale" : "1x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
"scale" : "2x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "83.5x83.5"
},
{
"idiom" : "ios-marketing",
"scale" : "1x",
"size" : "1024x1024"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "xcode",
"version" : 1
}
}
}

0 comments on commit bbbfac3

Please sign in to comment.