osm-android-compose icon indicating copy to clipboard operation
osm-android-compose copied to clipboard

Unexpected zooming and panning

Open cfbeard opened this issue 2 years ago • 3 comments

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

cfbeard avatar Nov 16 '23 19:11 cfbeard

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

sepehrpg avatar Jul 17 '24 07:07 sepehrpg

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.

cvetyshayasiren avatar Jul 30 '24 12:07 cvetyshayasiren

Instead of recreate to CameraState (which loose to map: OsmMapView property connection) i recommend to update the zoom level on motion events: MapViewUpdater.kt:23

  factory = {
            MapPropertiesNode(mapViewComposed, mapListeners, cameraState, overlayManagerState).also {
                mapViewComposed.setOnTouchListener { _, motionEvent: MotionEvent ->
                    if (motionEvent.action == MotionEvent.ACTION_UP) {
                        cameraState.geoPoint = GeoPoint(mapViewComposed.mapCenter.latitude, mapViewComposed.mapCenter.longitude)
                        cameraState.zoom = mapViewComposed.zoomLevelDouble
                    }
                    false
                }
            }
        }

This updates the zoom level correctly on pinch zoom.

rdietrichberlin avatar Sep 23 '24 07:09 rdietrichberlin