From ceb377d8a8749e7ee4dadb90066e3c54bfaf49de Mon Sep 17 00:00:00 2001 From: Olof Hedman Date: Wed, 22 Oct 2025 15:36:29 +0200 Subject: [PATCH] Fix map not center correctly on current location marker in WriteDocDialog --- .../app/klottr/maps/MapContainer.ios.kt | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/shared/src/iosMain/kotlin/app/klottr/maps/MapContainer.ios.kt b/shared/src/iosMain/kotlin/app/klottr/maps/MapContainer.ios.kt index 6670217..8029710 100644 --- a/shared/src/iosMain/kotlin/app/klottr/maps/MapContainer.ios.kt +++ b/shared/src/iosMain/kotlin/app/klottr/maps/MapContainer.ios.kt @@ -19,7 +19,8 @@ import cocoapods.GoogleMaps.* // from GoogleMaps iOS SDK import kotlinx.cinterop.CValue import kotlinx.cinterop.readValue import kotlinx.cinterop.useContents -import platform.CoreGraphics.CGRect +import platform.CoreGraphics.CGRectGetHeight +import platform.CoreGraphics.CGRectGetWidth import platform.UIKit.UIView import platform.darwin.NSObject @@ -51,13 +52,24 @@ private class IOSMapController( } } -private class MapContainerView(val mapView: GMSMapView) : UIView(frame = CGRectZero.readValue()) { + +private class MapContainerView( + val mapView: GMSMapView, + private val onFirstSized: (GMSMapView) -> Unit +) : UIView(frame = CGRectZero.readValue()) { + private var didNotifySized = false override fun layoutSubviews() { super.layoutSubviews() // Fill the container’s bounds whenever Compose changes our size. mapView.setFrame(frame = bounds) mapView.setNeedsLayout() mapView.layoutIfNeeded() + if (!didNotifySized && + CGRectGetWidth(bounds) > 0.0 && + CGRectGetHeight(bounds) > 0.0) { + didNotifySized = true + onFirstSized(mapView) + } } } @@ -105,10 +117,17 @@ actual fun MapContainer( }) // Wrap the map in a container that handles resizing via layoutSubviews. - val container = MapContainerView(mv) + val container = MapContainerView(mv) { sizedMap -> + // First time we actually have a size: apply features & ensure camera + applyFeatures(sizedMap, features) + // Re-set the camera once sized to avoid zero-projection glitches + sizedMap.camera = GMSCameraPosition.cameraWithTarget( + CLLocationCoordinate2DMake(state.camera.target.latitude, state.camera.target.longitude), + state.camera.zoom + ) + } container.addSubview(mv) // initial features - applyFeatures(mv, features) mapViewRef = mv container }, @@ -129,7 +148,11 @@ actual fun MapContainer( // re-apply features if list changed if (features !== state.features) { features = state.features - applyFeatures(map, features) + // Only apply once the map has a real size + val hasSize = CGRectGetWidth(map.bounds) > 0.0 && CGRectGetHeight(map.bounds) > 0.0 + if (hasSize) { + applyFeatures(map, features) + } } } },