Migrating to using wrapper MapContainer instead of Google Maps directly
This commit is contained in:
@@ -160,17 +160,7 @@ import coil3.compose.setSingletonImageLoaderFactory
|
||||
import coil3.toBitmap
|
||||
import com.google.android.gms.common.api.ApiException
|
||||
import com.google.android.gms.location.Priority
|
||||
import com.google.android.gms.maps.CameraUpdateFactory
|
||||
import com.google.android.gms.maps.model.CameraPosition
|
||||
import com.google.android.gms.maps.model.LatLng
|
||||
import com.google.maps.android.compose.GoogleMap
|
||||
import com.google.maps.android.compose.MapEffect
|
||||
import com.google.maps.android.compose.MapProperties
|
||||
import com.google.maps.android.compose.MapType
|
||||
import com.google.maps.android.compose.MapUiSettings
|
||||
import com.google.maps.android.compose.Marker
|
||||
import com.google.maps.android.compose.MarkerState
|
||||
import com.google.maps.android.compose.rememberCameraPositionState
|
||||
// Maps replaced by platform-independent MapContainer
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.tasks.await
|
||||
@@ -179,6 +169,14 @@ import java.io.File
|
||||
import java.util.UUID
|
||||
import kotlin.math.roundToInt
|
||||
import com.google.android.gms.location.LocationServices as GmsLocationServices
|
||||
import app.klottr.maps.CameraPosition as CrossCameraPosition
|
||||
import app.klottr.maps.LatLng as CrossLatLng
|
||||
import app.klottr.maps.MapController
|
||||
import app.klottr.maps.MapContainer
|
||||
import app.klottr.maps.MapFeature
|
||||
import app.klottr.maps.MapState
|
||||
import app.klottr.maps.MapUiSettings
|
||||
import app.klottr.maps.MapType as CrossMapType
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
|
||||
@@ -1239,13 +1237,31 @@ private fun WriteDocDialog(
|
||||
var titleError by remember { mutableStateOf<String?>(null) }
|
||||
val titleFocus = remember { FocusRequester() }
|
||||
|
||||
|
||||
val camState = rememberCameraPositionState {
|
||||
val lat = selectedLat ?: 59.3293
|
||||
val lon = selectedLon ?: 18.0686
|
||||
position = CameraPosition.fromLatLngZoom(LatLng(lat, lon), 20f) // max-zoom
|
||||
}
|
||||
val satellite = SettingsStore.getSatelliteMap()
|
||||
val initialCam = remember(selectedLat, selectedLon) {
|
||||
val lat = selectedLat ?: 59.3293
|
||||
val lon = selectedLon ?: 18.0686
|
||||
CrossCameraPosition(CrossLatLng(lat, lon), 18f)
|
||||
}
|
||||
val mapState = remember(initialCam, satellite) {
|
||||
MapState(
|
||||
initialCamera = initialCam,
|
||||
initialUi = MapUiSettings(
|
||||
myLocationButtonEnabled = false,
|
||||
zoomControlsEnabled = false,
|
||||
compassEnabled = false,
|
||||
mapType = if (satellite) CrossMapType.SATELLITE else CrossMapType.NORMAL
|
||||
),
|
||||
initialFeatures = listOfNotNull(
|
||||
selectedLat?.let { la ->
|
||||
selectedLon?.let { lo ->
|
||||
MapFeature.Marker(id = "sel", position = CrossLatLng(la, lo))
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
var mapController by remember { mutableStateOf<MapController?>(null) }
|
||||
|
||||
AlertDialog(
|
||||
onDismissRequest = onClose,
|
||||
@@ -1262,30 +1278,13 @@ private fun WriteDocDialog(
|
||||
.height(180.dp) // fast höjd → korrekt mätning, undviker "svart"
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
) {
|
||||
GoogleMap(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
cameraPositionState = camState,
|
||||
properties = MapProperties(
|
||||
isMyLocationEnabled = hasFgLocationPerms(context),
|
||||
mapType = if (satellite) MapType.SATELLITE else MapType.NORMAL
|
||||
),
|
||||
uiSettings = MapUiSettings(
|
||||
myLocationButtonEnabled = false,
|
||||
zoomControlsEnabled = false,
|
||||
compassEnabled = false,
|
||||
tiltGesturesEnabled = false,
|
||||
rotationGesturesEnabled = false,
|
||||
scrollGesturesEnabled = false,
|
||||
zoomGesturesEnabled = false
|
||||
)
|
||||
) {
|
||||
var position = LatLng(selectedLat!!, selectedLon!!)
|
||||
|
||||
MapEffect(position) { map ->
|
||||
map.animateCamera(CameraUpdateFactory.newLatLngZoom(position, 16f))
|
||||
MapContainer(
|
||||
state = mapState,
|
||||
onReady = { ctrl ->
|
||||
mapController = ctrl
|
||||
ctrl.setMyLocationEnabled(hasFgLocationPerms(context))
|
||||
}
|
||||
Marker(state = MarkerState(position = LatLng(selectedLat!!, selectedLon!!)))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1311,6 +1310,15 @@ private fun WriteDocDialog(
|
||||
selectedLat = L.latitude
|
||||
selectedLon = L.longitude
|
||||
lastPosUpdateMs = System.currentTimeMillis()
|
||||
// update marker + camera
|
||||
val la = L.latitude
|
||||
val lo = L.longitude
|
||||
mapState.features = listOf(
|
||||
MapFeature.Marker(id = "sel", position = CrossLatLng(la, lo))
|
||||
)
|
||||
mapController?.animateCamera(
|
||||
CrossCameraPosition(CrossLatLng(la, lo), 16f)
|
||||
)
|
||||
}
|
||||
}
|
||||
) { Text("Uppdatera position") }
|
||||
|
||||
@@ -18,6 +18,14 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalConfiguration
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import app.klottr.maps.CameraPosition
|
||||
import app.klottr.maps.LatLng as SharedLatLng
|
||||
import app.klottr.maps.MapContainer
|
||||
import app.klottr.maps.MapController
|
||||
import app.klottr.maps.MapFeature
|
||||
import app.klottr.maps.MapState
|
||||
import app.klottr.maps.MapType
|
||||
import app.klottr.maps.MapUiSettings
|
||||
import app.klottr.net.DigitaltMuseum
|
||||
import app.klottr.net.DigitaltMuseumItem
|
||||
import app.klottr.net.FornsokItem
|
||||
@@ -31,18 +39,6 @@ import app.klottr.util.SettingsStore
|
||||
import app.klottr.util.distanceMeters
|
||||
import com.google.android.gms.location.LocationServices as GmsLocationServices
|
||||
import com.google.android.gms.location.Priority
|
||||
import com.google.android.gms.maps.model.CameraPosition
|
||||
import com.google.android.gms.maps.model.LatLng
|
||||
import com.google.android.gms.maps.CameraUpdateFactory
|
||||
import com.google.maps.android.compose.CameraPositionState
|
||||
import com.google.maps.android.compose.GoogleMap
|
||||
import com.google.maps.android.compose.MapProperties
|
||||
import com.google.maps.android.compose.MapType
|
||||
import com.google.maps.android.compose.MarkerInfoWindow
|
||||
import com.google.maps.android.compose.MarkerState
|
||||
import com.google.maps.android.compose.MapUiSettings
|
||||
import com.google.maps.android.compose.rememberCameraPositionState
|
||||
import com.google.maps.android.compose.MapEffect
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.tasks.await
|
||||
import kotlinx.coroutines.withContext
|
||||
@@ -86,12 +82,27 @@ fun MapDialog(
|
||||
var selId by remember { mutableStateOf<String?>(null) } // klotter-id vid behov
|
||||
var selLayer by remember { mutableStateOf<String?>(null) } // "Klotter", "Wikipedia", "Fornsök", ...
|
||||
|
||||
// Map state for shared MapContainer
|
||||
val mapState = remember {
|
||||
MapState(
|
||||
initialCamera = CameraPosition(
|
||||
target = SharedLatLng(
|
||||
initialLat ?: 59.3293,
|
||||
initialLon ?: 18.0686
|
||||
),
|
||||
zoom = 16f
|
||||
),
|
||||
initialUi = MapUiSettings(
|
||||
compassEnabled = true,
|
||||
zoomControlsEnabled = false,
|
||||
myLocationButtonEnabled = hasFgLocationPerms(context),
|
||||
mapType = if (satellite) MapType.SATELLITE else MapType.NORMAL
|
||||
)
|
||||
)
|
||||
}
|
||||
var mapController by remember { mutableStateOf<MapController?>(null) }
|
||||
|
||||
|
||||
val camState: CameraPositionState = rememberCameraPositionState {
|
||||
val lat = initialLat ?: myLat ?: 59.3293
|
||||
val lon = initialLon ?: myLon ?: 18.0686
|
||||
position = CameraPosition.fromLatLngZoom(LatLng(lat, lon), 16f)
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
val fused = GmsLocationServices.getFusedLocationProviderClient(context)
|
||||
@@ -102,11 +113,27 @@ fun MapDialog(
|
||||
if (loc != null) {
|
||||
myLat = loc.latitude
|
||||
myLon = loc.longitude
|
||||
} else {
|
||||
myLat = 59.3293
|
||||
myLon = 18.0686
|
||||
}
|
||||
}
|
||||
//
|
||||
//
|
||||
// // Recenter camera when location arrives (only if no explicit initialLat/initialLon)
|
||||
// LaunchedEffect(mapController, myLat, myLon) {
|
||||
// val ctrl = mapController ?: return@LaunchedEffect
|
||||
// if (myLat != null && myLon != null && initialLat == null && initialLon == null) {
|
||||
// ctrl.animateCamera(
|
||||
// CameraPosition(target = SharedLatLng(myLat!!, myLon!!), zoom = 16f)
|
||||
// )
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Keep map features up-to-date as data/toggles change
|
||||
// LaunchedEffect(
|
||||
// mapController, showKlotter, showWiki, wikiRadius, showDm, dmRadius, showFornsok,
|
||||
// fornsokRadius, klotter, wiki, dm, forn
|
||||
// ) {
|
||||
// mapController?.setFeatures(buildFeatures())
|
||||
// }
|
||||
|
||||
LaunchedEffect(myLat, myLon, showKlotter, showWiki, wikiRadius, showDm, dmRadius, showFornsok, fornsokRadius) {
|
||||
val lat = myLat; val lon = myLon
|
||||
@@ -137,6 +164,59 @@ fun MapDialog(
|
||||
} else forn = emptyList()
|
||||
}
|
||||
|
||||
// Helper: build features for the map
|
||||
fun buildFeatures(): List<MapFeature> {
|
||||
val lat = myLat; val lon = myLon
|
||||
val out = mutableListOf<MapFeature>()
|
||||
if (showKlotter) {
|
||||
klotter.forEach { n ->
|
||||
val d = if (lat != null && lon != null)
|
||||
distanceMeters(lat, lon, n.a, n.o).roundToInt() else Int.MAX_VALUE
|
||||
val titleLine = n.t.substringBefore('\n').ifBlank { "Okänt klotter" }
|
||||
val radius = Config.DEFAULT_KLOTTER_RADIUS_M
|
||||
val visited = isKnownNote(n.i.toLong())
|
||||
val within = (d != Int.MAX_VALUE && d <= radius)
|
||||
val display = if (visited || (within && titleLine != "Okänt klotter")) titleLine else "Okänt klotter"
|
||||
out += MapFeature.Marker(
|
||||
id = "klotter:${n.i}",
|
||||
position = SharedLatLng(n.a, n.o),
|
||||
title = display
|
||||
)
|
||||
}
|
||||
}
|
||||
if (showWiki) {
|
||||
wiki.forEach { w ->
|
||||
out += MapFeature.Marker(
|
||||
id = "wiki:${w.pageId}",
|
||||
position = SharedLatLng(w.lat, w.lon),
|
||||
title = w.title
|
||||
)
|
||||
}
|
||||
}
|
||||
if (showDm) {
|
||||
dm.forEach { dmi ->
|
||||
val dmId = dmi.webUrl ?: dmi.url ?: "${dmi.lat},${dmi.lon}"
|
||||
out += MapFeature.Marker(
|
||||
id = "dm:$dmId",
|
||||
position = SharedLatLng(dmi.lat, dmi.lon),
|
||||
title = dmi.title
|
||||
)
|
||||
}
|
||||
}
|
||||
if (showFornsok) {
|
||||
forn.forEach { f ->
|
||||
val fid = f.uri ?: f.title
|
||||
out += MapFeature.Marker(
|
||||
id = "forn:$fid",
|
||||
position = SharedLatLng(f.lat, f.lon),
|
||||
title = f.title
|
||||
)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
|
||||
AlertDialog(
|
||||
onDismissRequest = onClose,
|
||||
title = { Text("Karta") },
|
||||
@@ -147,197 +227,67 @@ fun MapDialog(
|
||||
.heightIn(min = 320.dp, max = dialogHeight)
|
||||
.fillMaxWidth()
|
||||
) {
|
||||
GoogleMap(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
cameraPositionState = camState,
|
||||
properties = MapProperties(
|
||||
isMyLocationEnabled = hasFgLocationPerms(context),
|
||||
mapType = if (satellite) MapType.SATELLITE else MapType.NORMAL
|
||||
),
|
||||
uiSettings = MapUiSettings(
|
||||
myLocationButtonEnabled = true,
|
||||
zoomControlsEnabled = false,
|
||||
compassEnabled = true
|
||||
)
|
||||
) {
|
||||
MapEffect(myLat, myLon) { map ->
|
||||
val lat = myLat; val lon = myLon
|
||||
if (lat != null && lon != null) {
|
||||
map.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(lat, lon), 16f))
|
||||
// Shared Map
|
||||
MapContainer(
|
||||
state = mapState,
|
||||
onMapClick = {
|
||||
// Clear selection
|
||||
selLat = null; selLon = null; selTitle = null; selUri = null; selId = null; selLayer = null
|
||||
},
|
||||
onMarkerClick = { id ->
|
||||
fun setSel(lat: Double, lon: Double, title: String?, uri: String?, layer: String, itemId: String? = null) {
|
||||
selLat = lat; selLon = lon; selTitle = title; selUri = uri; selLayer = layer; selId = itemId
|
||||
}
|
||||
}
|
||||
|
||||
val lat = myLat; val lon = myLon
|
||||
if (showKlotter) {
|
||||
klotter.forEach { n ->
|
||||
val d = if (lat != null && lon != null)
|
||||
distanceMeters(lat, lon, n.a, n.o).roundToInt() else Int.MAX_VALUE
|
||||
|
||||
val titleLine = n.t.substringBefore('\n').ifBlank { "Okänt klotter" }
|
||||
val radius = Config.DEFAULT_KLOTTER_RADIUS_M
|
||||
val visited = isKnownNote(n.i.toLong())
|
||||
val within = (d != Int.MAX_VALUE && d <= radius)
|
||||
val display = if (visited || (within && titleLine != "Okänt klotter")) titleLine else "Okänt klotter"
|
||||
|
||||
MarkerInfoWindow(
|
||||
state = MarkerState(position = LatLng(n.a, n.o)),
|
||||
onClick = {
|
||||
// Sätt koordinater + titel så “Maps”/“Vägbeskrivning” får rätt namn
|
||||
selLat = n.a; selLon = n.o; selTitle = display; selUri = null
|
||||
false // låt infowindow visas
|
||||
},
|
||||
onInfoWindowClick = {
|
||||
// Öppna detaljrutan (samma som i Hittat/Närliggande)
|
||||
onKlotterClick(n.i.toString(), n.a, n.o, display)
|
||||
when {
|
||||
id.startsWith("klotter:") -> {
|
||||
val nid = id.removePrefix("klotter:")
|
||||
klotter.firstOrNull { it.i.toString() == nid }?.let { n ->
|
||||
val titleLine = n.t.substringBefore('\n').ifBlank { "Okänt klotter" }
|
||||
setSel(n.a, n.o, titleLine, null, "Klotter", nid)
|
||||
onKlotterClick(nid, n.a, n.o, titleLine)
|
||||
}
|
||||
) {
|
||||
// Läslig bubblan med bakgrund
|
||||
Card(
|
||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
|
||||
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
|
||||
shape = RoundedCornerShape(8.dp)
|
||||
) {
|
||||
Column(Modifier.padding(8.dp)) {
|
||||
Text("Klotter", style = MaterialTheme.typography.labelMedium)
|
||||
Text(display, style = MaterialTheme.typography.bodyMedium)
|
||||
Text(
|
||||
"Läs mer",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
modifier = Modifier.clickable {
|
||||
onKlotterClick(n.i.toString(), n.a, n.o, display)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
id.startsWith("wiki:") -> {
|
||||
val pid = id.removePrefix("wiki:")
|
||||
wiki.firstOrNull { it.pageId.toString() == pid }?.let { w ->
|
||||
val url = "https://sv.m.wikipedia.org/?curid=${w.pageId}"
|
||||
setSel(w.lat, w.lon, w.title, url, "Wikipedia")
|
||||
onOpenUrl(w.title, url)
|
||||
}
|
||||
}
|
||||
id.startsWith("dm:") -> {
|
||||
val mid = id.removePrefix("dm:")
|
||||
dm.firstOrNull { (it.webUrl ?: it.url) == mid }?.let { dmi ->
|
||||
val url = dmi.webUrl ?: dmi.url
|
||||
setSel(dmi.lat, dmi.lon, dmi.title, url, "DigitaltMuseum")
|
||||
url?.let { onOpenUrl(dmi.title, it) }
|
||||
}
|
||||
}
|
||||
id.startsWith("forn:") -> {
|
||||
val fid = id.removePrefix("forn:")
|
||||
forn.firstOrNull { (it.uri ?: it.title) == fid }?.let { f ->
|
||||
setSel(f.lat, f.lon, f.title, f.uri, "Fornsök")
|
||||
f.uri?.let { onOpenUrl(f.title, it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
onReady = { ctrl ->
|
||||
mapController = ctrl
|
||||
ctrl.setMyLocationEnabled(hasFgLocationPerms(context))
|
||||
ctrl.setFeatures(buildFeatures())
|
||||
}
|
||||
|
||||
|
||||
if (showWiki) {
|
||||
wiki.forEach { w ->
|
||||
val d = if (lat != null && lon != null)
|
||||
distanceMeters(lat, lon, w.lat, w.lon).roundToInt() else Int.MAX_VALUE
|
||||
MarkerInfoWindow(
|
||||
state = MarkerState(position = LatLng(w.lat, w.lon)),
|
||||
onClick = {
|
||||
selLat = w.lat; selLon = w.lon; selTitle = null; selUri = "https://sv.m.wikipedia.org/?curid=${w.pageId}"
|
||||
false
|
||||
},
|
||||
onInfoWindowClick = {
|
||||
onOpenUrl(w.title, "https://sv.m.wikipedia.org/?curid=${w.pageId}")
|
||||
}
|
||||
) {
|
||||
Card(
|
||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
|
||||
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
|
||||
shape = RoundedCornerShape(8.dp)
|
||||
) {
|
||||
Column(Modifier.padding(8.dp)) {
|
||||
Text("Wikipedia", style = MaterialTheme.typography.labelMedium)
|
||||
Text(w.title, style = MaterialTheme.typography.bodyMedium)
|
||||
Text("Läs mer", style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (showDm) {
|
||||
dm.forEach { dmi ->
|
||||
val d = if (lat != null && lon != null)
|
||||
distanceMeters(lat, lon, dmi.lat, dmi.lon).roundToInt() else Int.MAX_VALUE
|
||||
|
||||
MarkerInfoWindow(
|
||||
state = MarkerState(position = LatLng(dmi.lat, dmi.lon)),
|
||||
onClick = {
|
||||
// Sätt samma state som för Wiki/Fornsök så knapparna nere till höger aktiveras
|
||||
selLat = dmi.lat
|
||||
selLon = dmi.lon
|
||||
selTitle = dmi.title
|
||||
selUri = dmi.webUrl ?: dmi.url // objektsidan om den finns, annars bild-URL
|
||||
false // låt infowindow visas
|
||||
},
|
||||
onInfoWindowClick = {
|
||||
// Öppna “Läs mer” på samma sätt som Wiki/Fornsök
|
||||
(dmi.webUrl ?: dmi.url)?.let { onOpenUrl(dmi.title, it) }
|
||||
}
|
||||
) {
|
||||
Card(
|
||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
|
||||
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
|
||||
shape = RoundedCornerShape(8.dp)
|
||||
) {
|
||||
Column(Modifier.padding(8.dp)) {
|
||||
Text("DigitaltMuseum", style = MaterialTheme.typography.labelMedium)
|
||||
Text(dmi.title, style = MaterialTheme.typography.bodyMedium)
|
||||
Text(
|
||||
"Läs mer",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
modifier = Modifier.clickable {
|
||||
(dmi.webUrl ?: dmi.url)?.let { onOpenUrl(dmi.title, it) }
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (showFornsok) {
|
||||
forn.forEach { f ->
|
||||
val d = if (lat != null && lon != null)
|
||||
distanceMeters(lat, lon, f.lat, f.lon).roundToInt() else Int.MAX_VALUE
|
||||
val header = f.title // vi har inte 'type' längre
|
||||
MarkerInfoWindow(
|
||||
state = MarkerState(position = LatLng(f.lat, f.lon)),
|
||||
onClick = { selLat = f.lat; selLon = f.lon; selTitle = null; selUri = f.uri; false },
|
||||
onInfoWindowClick = { f.uri?.let { onOpenUrl(f.title, it) } }
|
||||
) {
|
||||
Card(
|
||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
|
||||
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
|
||||
shape = RoundedCornerShape(8.dp)
|
||||
) {
|
||||
Column(Modifier.padding(8.dp)) {
|
||||
Text("Fornsök", style = MaterialTheme.typography.labelMedium)
|
||||
Text(f.title, style = MaterialTheme.typography.bodyMedium)
|
||||
Text("Läs mer", style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.align(Alignment.BottomEnd)
|
||||
.padding(8.dp),
|
||||
horizontalAlignment = Alignment.End
|
||||
) {
|
||||
|
||||
Button(
|
||||
onClick = { openInMaps(context, selLat, selLon, selTitle) },
|
||||
enabled = selLat != null && selLon != null
|
||||
) { Text("Maps") }
|
||||
|
||||
Spacer(Modifier.height(8.dp))
|
||||
|
||||
Button(
|
||||
onClick = { openWalkingDirections(context, selLat, selLon, selTitle) },
|
||||
enabled = selLat != null && selLon != null
|
||||
) { Text("Vägbeskrivning") }
|
||||
|
||||
Spacer(Modifier.height(8.dp))
|
||||
|
||||
Button(
|
||||
onClick = {
|
||||
selUri?.let { context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(it))) }
|
||||
},
|
||||
enabled = selUri != null
|
||||
) { Text("Läs mer") }
|
||||
}
|
||||
)
|
||||
// Keep camera + markers in sync with state changes
|
||||
MapSyncEffects(
|
||||
controller = mapController,
|
||||
myLat = if (initialLat == null && initialLon == null) myLat else null, // don't override explicit initial cam
|
||||
myLon = if (initialLat == null && initialLon == null) myLon else null,
|
||||
featuresBuilder = { buildFeatures() },
|
||||
// everything that affects markers:
|
||||
showKlotter, showWiki, wikiRadius, showDm, dmRadius, showFornsok, fornsokRadius,
|
||||
klotter, wiki, dm, forn
|
||||
)
|
||||
}
|
||||
},
|
||||
confirmButton = {
|
||||
@@ -372,3 +322,24 @@ private fun openWalkingDirections(ctx: Context, lat: Double?, lon: Double?, titl
|
||||
openInMaps(ctx, lat, lon, title)
|
||||
}
|
||||
}
|
||||
|
||||
// Keep map in sync with state changes
|
||||
@Composable
|
||||
private fun MapSyncEffects(
|
||||
controller: MapController?,
|
||||
myLat: Double?, myLon: Double?,
|
||||
featuresBuilder: () -> List<MapFeature>,
|
||||
vararg featuresDeps: Any?
|
||||
) {
|
||||
LaunchedEffect(controller, myLat, myLon) {
|
||||
if (controller != null && myLat != null && myLon != null) {
|
||||
controller.animateCamera(CameraPosition(SharedLatLng(myLat, myLon), 16f))
|
||||
}
|
||||
}
|
||||
// Rebuild + apply features whenever any dependency changes
|
||||
LaunchedEffect(controller, *featuresDeps) {
|
||||
if (controller != null) {
|
||||
controller.setFeatures(featuresBuilder())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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