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.CValue
import kotlinx.cinterop.readValue import kotlinx.cinterop.readValue
import kotlinx.cinterop.useContents import kotlinx.cinterop.useContents
import platform.CoreGraphics.CGRect import platform.CoreGraphics.CGRectGetHeight
import platform.CoreGraphics.CGRectGetWidth
import platform.UIKit.UIView import platform.UIKit.UIView
import platform.darwin.NSObject 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() { override fun layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
// Fill the container’s bounds whenever Compose changes our size. // Fill the container’s bounds whenever Compose changes our size.
mapView.setFrame(frame = bounds) mapView.setFrame(frame = bounds)
mapView.setNeedsLayout() mapView.setNeedsLayout()
mapView.layoutIfNeeded() 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. // 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) container.addSubview(mv)
// initial features // initial features
applyFeatures(mv, features)
mapViewRef = mv mapViewRef = mv
container container
}, },
@@ -129,7 +148,11 @@ actual fun MapContainer(
// re-apply features if list changed // re-apply features if list changed
if (features !== state.features) { if (features !== state.features) {
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)
}
} }
} }
}, },