Migrating to using wrapper MapContainer instead of Google Maps directly
This commit is contained in:
@@ -4,6 +4,8 @@ import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import com.google.android.gms.maps.CameraUpdateFactory
|
||||
import com.google.maps.android.compose.GoogleMap
|
||||
import com.google.maps.android.compose.MapProperties
|
||||
import com.google.maps.android.compose.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.channels.BufferOverflow
|
||||
@@ -16,10 +18,23 @@ private class AndroidMapController(
|
||||
private val onSetFeatures: (List<MapFeature>) -> Unit,
|
||||
private val myLocationSetter: (Boolean) -> Unit,
|
||||
private val scope: CoroutineScope,
|
||||
private val isReady: () -> Boolean,
|
||||
) : MapController {
|
||||
private val _camera = MutableSharedFlow<CameraPosition>(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
|
||||
override val cameraPosition = _camera.asSharedFlow()
|
||||
private var pendingCamera: CameraPosition? = null
|
||||
|
||||
fun flushPendingIfAny() {
|
||||
val pos = pendingCamera ?: return
|
||||
if (!isReady()) return
|
||||
pendingCamera = null
|
||||
animateCamera(pos)
|
||||
}
|
||||
override fun moveCamera(position: CameraPosition) {
|
||||
if (!isReady()) {
|
||||
pendingCamera = position
|
||||
return
|
||||
}
|
||||
cameraState.move(
|
||||
CameraUpdateFactory.newCameraPosition(
|
||||
com.google.android.gms.maps.model.CameraPosition.fromLatLngZoom(
|
||||
@@ -30,6 +45,10 @@ private class AndroidMapController(
|
||||
}
|
||||
override fun animateCamera(position: CameraPosition) {
|
||||
// Fire-and-forget on the main scope (rememberCoroutineScope()).
|
||||
if (!isReady()) {
|
||||
pendingCamera = position
|
||||
return
|
||||
}
|
||||
scope.launch {
|
||||
cameraState.animate(
|
||||
CameraUpdateFactory.newCameraPosition(
|
||||
@@ -63,13 +82,15 @@ actual fun MapContainer(
|
||||
|
||||
var features by remember { mutableStateOf(state.features) }
|
||||
var myLocationEnabled by remember { mutableStateOf(false) }
|
||||
var mapLoaded by remember { mutableStateOf(false) }
|
||||
|
||||
val controller = remember(cameraState) {
|
||||
AndroidMapController(
|
||||
cameraState = cameraState,
|
||||
onSetFeatures = { features = it },
|
||||
myLocationSetter = { myLocationEnabled = it },
|
||||
scope = scope
|
||||
scope = scope,
|
||||
isReady = { mapLoaded }
|
||||
)
|
||||
}
|
||||
|
||||
@@ -86,11 +107,21 @@ actual fun MapContainer(
|
||||
zoomControlsEnabled = state.ui.zoomControlsEnabled,
|
||||
myLocationButtonEnabled = state.ui.myLocationButtonEnabled
|
||||
),
|
||||
properties = MapProperties(isMyLocationEnabled = myLocationEnabled),
|
||||
properties = MapProperties(
|
||||
isMyLocationEnabled = myLocationEnabled,
|
||||
mapType = when (state.ui.mapType) {
|
||||
MapType.SATELLITE -> com.google.maps.android.compose.MapType.SATELLITE
|
||||
MapType.NORMAL -> com.google.maps.android.compose.MapType.NORMAL
|
||||
}
|
||||
),
|
||||
onMapClick = { latLng ->
|
||||
onMapClick?.invoke(LatLng(latLng.latitude, latLng.longitude))
|
||||
},
|
||||
onPOIClick = { /* optional */ },
|
||||
onMapLoaded = {
|
||||
mapLoaded = true
|
||||
controller.flushPendingIfAny()
|
||||
},
|
||||
) {
|
||||
features.forEach { f ->
|
||||
when (f) {
|
||||
|
||||
@@ -17,10 +17,15 @@ sealed interface MapFeature {
|
||||
// Add Polyline/Polygon later if needed
|
||||
}
|
||||
|
||||
enum class MapType {
|
||||
NORMAL, SATELLITE
|
||||
}
|
||||
|
||||
data class MapUiSettings(
|
||||
val compassEnabled: Boolean = true,
|
||||
val zoomControlsEnabled: Boolean = false,
|
||||
val myLocationButtonEnabled: Boolean = false,
|
||||
val mapType: MapType = MapType.NORMAL,
|
||||
)
|
||||
|
||||
interface MapController {
|
||||
|
||||
@@ -70,6 +70,10 @@ actual fun MapContainer(
|
||||
mv.settings.zoomGestures = true
|
||||
mv.settings.scrollGestures = true
|
||||
mv.settings.myLocationButton = state.ui.myLocationButtonEnabled
|
||||
mv.mapType = when (state.ui.mapType) {
|
||||
MapType.SATELLITE -> kGMSTypeSatellite
|
||||
MapType.NORMAL -> kGMSTypeNormal
|
||||
}
|
||||
mv.setDelegate(object : NSObject(), GMSMapViewDelegateProtocol {
|
||||
|
||||
|
||||
@@ -98,6 +102,10 @@ actual fun MapContainer(
|
||||
// update UI toggles
|
||||
map.settings.compassButton = state.ui.compassEnabled
|
||||
map.settings.myLocationButton = state.ui.myLocationButtonEnabled
|
||||
map.mapType = when (state.ui.mapType) {
|
||||
MapType.SATELLITE -> kGMSTypeSatellite
|
||||
MapType.NORMAL -> kGMSTypeNormal
|
||||
}
|
||||
// update camera if changed (best-effort)
|
||||
// (Optional: compare current camera to state.camera)
|
||||
// re-apply features if list changed
|
||||
|
||||
Reference in New Issue
Block a user