apple_maps_flutter icon indicating copy to clipboard operation
apple_maps_flutter copied to clipboard

Camera move always adjusts zoom

Open kbot opened this issue 1 year ago • 2 comments

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 avatar Dec 19 '24 21:12 kbot

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
}

kbot avatar Dec 20 '24 00:12 kbot

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

winter-tech avatar Dec 20 '24 06:12 winter-tech