utsmannn / osm-android-compose

OpenStreetMap android for compose
https://utsmannn.github.io/osm-android-compose/
35 stars 10 forks source link

Unexpected zooming and panning #4

Open cfbeard opened 10 months ago

cfbeard commented 10 months ago

The lines attached below seem to be causing some unexpected zoom and panning issues. When zooming in, the camera will automatically zoom back out or pan in a random direction.

https://github.com/utsmannn/osm-android-compose/blob/8513df0e09c2e5f13e17e15ce9581446ff3025de/osm-compose/src/main/java/com/utsman/osmandcompose/OpenStreetMap.kt#L130-L135

sepehrpg commented 2 months ago

I also have this problem‌ "When zooming in, the camera will automatically zoom back out or pan in a random direction."

cvetyshayasiren commented 1 month ago

I encountered this issue and after many attempts, found a temporary working solution.

Instead of:

val cameraState = rememberCameraState {
    geoPoint = GeoPoint(60.0, 30.0)
    zoom = 14.0
}

Use:

var cameraState by remember { 
    mutableStateOf(
        CameraState(
            CameraProperty(
                geoPoint = GeoPoint(60.0, 30.0), 
                zoom = 14.0
            )
        )
    )
}

LaunchedEffect(cameraState.zoom) {
    val zoom = cameraState.zoom
    val geoPoint = cameraState.geoPoint
    cameraState = CameraState(
        CameraProperty(
            geoPoint = geoPoint,
            zoom = zoom
        )
    )
}

The key is to catch the zoom change event and "recreate" the cameraState before this random drift occurs.