Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

How to vary the pitch level depending on the camera altitude? Getting infinite loop and app crashes #14275

Closed
Robtles opened this issue Mar 31, 2019 · 6 comments
Labels
archived Archived because of inactivity bug iOS Mapbox Maps SDK for iOS

Comments

@Robtles
Copy link

Robtles commented Mar 31, 2019

Hello,

I am trying to get a "Pokemon GO"-like effect, with the camera pitch varying depending on its altitude. I am restricting the camera zoom level from 17 to 19, so it's always close to the ground.

Aerial view:

Capture d’écran 2019-03-30 à 13 50 48

Closer to the ground:

Capture d’écran 2019-03-30 à 13 50 32

I have written a simple linear function that I plan to improve, returning a value (the pitch) that should always be between ~40 (for a zoom level of 17) and ~100 (for a zoom level of 19).

func pitchFor(_ altitude: Double = 420) -> CGFloat {
    return CGFloat(100 - ((altitude - 125) / 6.85))
}

Then I simply thought it would be enough to call mapViewRegionIsChanging delegate function, and updating the pitch from here:

func mapViewRegionIsChanging(_ mapView: MGLMapView) {
    let camera = MGLMapCamera(lookingAtCenter: mapView.camera.centerCoordinate, 
                                     altitude: mapView.camera.altitude, 
                                        pitch: pitchFor(mapView.camera.altitude), 
                                      heading: mapView.camera.heading)
    mapView.setCamera(camera, animated: true)
}

But I end up getting something that looks a lot like an infinite loop and then the app crashes:

Capture d’écran 2019-03-30 à 16 12 55

Can you help me with that? I didn't know where to ask so I hope it's the right place, sorry if not.

Thanks for your help.

Configuration

Mapbox SDK versions: 4.9.0
iOS/macOS versions: 12.1.4
Device/simulator models: iPhone XR
Xcode version: 10.1

@1ec5
Copy link
Contributor

1ec5 commented Mar 31, 2019

Unfortunately, methods such as -mapViewRegionIsChanging: get called in response to programmatic camera changes in addition to user-initiated camera changes. So changing the camera within -mapViewRegionIsChanging: causes recursion unless you explicitly guard against it with a base case. For example, the method could bail if the pitch is already the same as what you’d change it to.

(Relatedly, #6908 tracks varying the maximum pitch by the altitude or zoom level.)

@1ec5 1ec5 added the iOS Mapbox Maps SDK for iOS label Mar 31, 2019
@Robtles
Copy link
Author

Robtles commented Apr 1, 2019

Thank you for your answer, and all the work you're doing on this project.
Is there maybe another way to achieve this varying pitch feature, maybe from another delegate method? I have read the related issue and it looks like there is no easy way to do so, or maybe have I missed something.

@julianrex
Copy link
Contributor

@StratRob until we fix the core underlying issue, I'd recommend trying to call setCamera from a DispatchQueue.main.async. See #14998 (comment) for a similar issue.

Can you please try this and let me know if this works for you? Thanks

@julianrex julianrex added the bug label Jul 3, 2019
@julianrex
Copy link
Contributor

/cc @pozdnyakov for visibility

@stale stale bot added the archived Archived because of inactivity label Jan 8, 2020
@stale
Copy link

stale bot commented Jan 8, 2020

This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions.

@stale stale bot closed this as completed Jan 8, 2020
@nnhubbard
Copy link

Was this issue ever fixed @julianrex ?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived Archived because of inactivity bug iOS Mapbox Maps SDK for iOS
Projects
None yet
Development

No branches or pull requests

4 participants