I'm trying to change the pitch to be more and more angled as the user zooms in, but I have a suspicion it's because the .cameraChanged event is firing fast all the time, so the app crashes on the initial map load. Any suggestions on figuring out how to make this work?
override public func viewDidLoad() {
let myResourceOptions = ResourceOptions(accessToken: "pk.ey###################")
let myCameraOptions = CameraOptions(center: appState.myLocation.coordinate, zoom: 12)
let myMapInitOptions = MapInitOptions(resourceOptions: myResourceOptions, cameraOptions: myCameraOptions, styleURI: StyleURI(url: URL(string: "mapbox://styles/myaccount/myMap")!)!)
let myCameraBoundsOptions = CameraBoundsOptions(maxZoom: 18.5, minZoom: 11, maxPitch: 60, minPitch: 0)
do {
try mapView.mapboxMap.setCameraBounds(with: myCameraBoundsOptions)
mapView.mapboxMap.onNext(event: .mapLoaded) { _ in
// add sources, layers, etc
}
mapView.mapboxMap.onEvery(event: .cameraChanged, handler: { [weak self] _ in
guard let self = self else { return }
let pitch = ((self.mapView.mapboxMap.cameraState.zoom - self.mapView.mapboxMap.cameraBounds.minZoom)/(self.mapView.mapboxMap.cameraBounds.maxZoom - self.mapView.mapboxMap.cameraBounds.minZoom)) * 60
let options = CameraOptions(pitch: pitch)
self.mapView.mapboxMap.setCamera(to: options)
})
}
}
by setting new pitch will result in another
cameraChangedevent, this will lead to an infinite loop and crash the program. What you can do to avoid this is to have a flag on when to set new pitch, for example: