Fix map not center correctly on current location marker in WriteDocDialog

This commit is contained in:
Olof Hedman
2025-10-22 15:36:29 +02:00
parent 6c37a4232d
commit ceb377d8a8

View File

@@ -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)
}
}
}
},