Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camera move always adjusts zoom #68

Open
kbot opened this issue Dec 19, 2024 · 2 comments
Open

Camera move always adjusts zoom #68

kbot opened this issue Dec 19, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@kbot
Copy link

kbot commented Dec 19, 2024

Describe the bug
When using cameraMove, the calculatedZoomLevel sets a new zoomLevel that is slightly different than the current value. This results in minor zoom when attempting to just move the camera

To Reproduce
Steps to reproduce the behavior:

  1. Go to 'Camera control'
  2. Click on 'newLatLng'
  3. click on 'newLatLng' additional times

Expected behavior
Lat long should move, camera zoom should stay constant

Screenshots
If applicable, add screenshots to help explain your problem.

Smartphone (please complete the following information):

  • Device: iPad 12.9 simulator
  • Version iOS 17.4

flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.27.1, on macOS 14.4.1 23E224 darwin-arm64 (Rosetta), locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.2)
[✓] VS Code (version 1.96.0)
[✓] Connected device (6 available)
[✓] Network resources

• No issues found!

@kbot kbot added the bug Something isn't working label Dec 19, 2024
@kbot
Copy link
Author

kbot commented Dec 20, 2024

looking at this a little deeper, I can see that getCameraAltitude and calculatedZoomLevel have minor differences that through somewhat circular logic cause the zooming. The altitude based on the zoomLevel adjust the visible map which changes the zoomLevel... and so on

I played around with some calculations and this seems to produce more consistent values:

static let earthCircumference: Double = 40_075_016.686  // Earth's circumference in meters
static let mapWidthAtZoom21: Double = earthCircumference / 2.0  // Half circumference at zoom level 21
static let fieldOfViewAngle: Double = 15.0 * .pi / 180.0  // Field of view in radians
var calculatedZoomLevel: Double {
    get {
        // Calculate distance per pixel at zoom level 21
        let screenScale = UIScreen.main.scale  // To account for Retina screens
        let distancePerPixelAtZoom21 = MKMapView.mapWidthAtZoom21 / (256.0 * pow(2.0, maxZoomLevel)) / screenScale

        // Calculate zoom scale
        let zoomScale = (camera.altitude * tan(MKMapView.fieldOfViewAngle)) / distancePerPixelAtZoom21

        // Derive zoom level
        let zoomLevel = maxZoomLevel - log2(zoomScale)
        return zoomLevel
    }
    set (newZoomLevel) {
        Holder._zoomLevel = newZoomLevel
    }
}
private func getCameraAltitude(centerCoordinate: CLLocationCoordinate2D, zoomLevel: Double) -> Double {
    // Calculate distance per pixel at zoom level 21
    let screenScale = UIScreen.main.scale  // To account for Retina screens
    let distancePerPixelAtZoom21 = MKMapView.mapWidthAtZoom21 / (256.0 * pow(2.0, maxZoomLevel)) / screenScale

    // Calculate zoom scale
    let zoomScale = pow(2.0, maxZoomLevel - zoomLevel)

    // Derive altitude
    let altitude = (distancePerPixelAtZoom21 * zoomScale) / tan(MKMapView.fieldOfViewAngle)
    return altitude
}

@winter-tech
Copy link

I also encountered this situation on my phone, but it was in landscape mode, while portrait mode is normal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants