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 coil3.toBitmap
|
||||||
import com.google.android.gms.common.api.ApiException
|
import com.google.android.gms.common.api.ApiException
|
||||||
import com.google.android.gms.location.Priority
|
import com.google.android.gms.location.Priority
|
||||||
import com.google.android.gms.maps.CameraUpdateFactory
|
// Maps replaced by platform-independent MapContainer
|
||||||
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
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.tasks.await
|
import kotlinx.coroutines.tasks.await
|
||||||
@@ -179,6 +169,14 @@ import java.io.File
|
|||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
import com.google.android.gms.location.LocationServices as GmsLocationServices
|
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() {
|
class MainActivity : ComponentActivity() {
|
||||||
|
|
||||||
@@ -1239,13 +1237,31 @@ private fun WriteDocDialog(
|
|||||||
var titleError by remember { mutableStateOf<String?>(null) }
|
var titleError by remember { mutableStateOf<String?>(null) }
|
||||||
val titleFocus = remember { FocusRequester() }
|
val titleFocus = remember { FocusRequester() }
|
||||||
|
|
||||||
|
val satellite = SettingsStore.getSatelliteMap()
|
||||||
val camState = rememberCameraPositionState {
|
val initialCam = remember(selectedLat, selectedLon) {
|
||||||
val lat = selectedLat ?: 59.3293
|
val lat = selectedLat ?: 59.3293
|
||||||
val lon = selectedLon ?: 18.0686
|
val lon = selectedLon ?: 18.0686
|
||||||
position = CameraPosition.fromLatLngZoom(LatLng(lat, lon), 20f) // max-zoom
|
CrossCameraPosition(CrossLatLng(lat, lon), 18f)
|
||||||
}
|
}
|
||||||
val satellite = SettingsStore.getSatelliteMap()
|
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(
|
AlertDialog(
|
||||||
onDismissRequest = onClose,
|
onDismissRequest = onClose,
|
||||||
@@ -1262,30 +1278,13 @@ private fun WriteDocDialog(
|
|||||||
.height(180.dp) // fast höjd → korrekt mätning, undviker "svart"
|
.height(180.dp) // fast höjd → korrekt mätning, undviker "svart"
|
||||||
.clip(RoundedCornerShape(8.dp))
|
.clip(RoundedCornerShape(8.dp))
|
||||||
) {
|
) {
|
||||||
GoogleMap(
|
MapContainer(
|
||||||
modifier = Modifier.fillMaxSize(),
|
state = mapState,
|
||||||
cameraPositionState = camState,
|
onReady = { ctrl ->
|
||||||
properties = MapProperties(
|
mapController = ctrl
|
||||||
isMyLocationEnabled = hasFgLocationPerms(context),
|
ctrl.setMyLocationEnabled(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))
|
|
||||||
}
|
|
||||||
Marker(state = MarkerState(position = LatLng(selectedLat!!, selectedLon!!)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1311,6 +1310,15 @@ private fun WriteDocDialog(
|
|||||||
selectedLat = L.latitude
|
selectedLat = L.latitude
|
||||||
selectedLon = L.longitude
|
selectedLon = L.longitude
|
||||||
lastPosUpdateMs = System.currentTimeMillis()
|
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") }
|
) { Text("Uppdatera position") }
|
||||||
|
|||||||
@@ -18,6 +18,14 @@ import androidx.compose.ui.Modifier
|
|||||||
import androidx.compose.ui.platform.LocalConfiguration
|
import androidx.compose.ui.platform.LocalConfiguration
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.unit.dp
|
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.DigitaltMuseum
|
||||||
import app.klottr.net.DigitaltMuseumItem
|
import app.klottr.net.DigitaltMuseumItem
|
||||||
import app.klottr.net.FornsokItem
|
import app.klottr.net.FornsokItem
|
||||||
@@ -31,18 +39,6 @@ import app.klottr.util.SettingsStore
|
|||||||
import app.klottr.util.distanceMeters
|
import app.klottr.util.distanceMeters
|
||||||
import com.google.android.gms.location.LocationServices as GmsLocationServices
|
import com.google.android.gms.location.LocationServices as GmsLocationServices
|
||||||
import com.google.android.gms.location.Priority
|
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.Dispatchers
|
||||||
import kotlinx.coroutines.tasks.await
|
import kotlinx.coroutines.tasks.await
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@@ -86,12 +82,27 @@ fun MapDialog(
|
|||||||
var selId by remember { mutableStateOf<String?>(null) } // klotter-id vid behov
|
var selId by remember { mutableStateOf<String?>(null) } // klotter-id vid behov
|
||||||
var selLayer by remember { mutableStateOf<String?>(null) } // "Klotter", "Wikipedia", "Fornsök", ...
|
var selLayer by remember { mutableStateOf<String?>(null) } // "Klotter", "Wikipedia", "Fornsök", ...
|
||||||
|
|
||||||
|
// Map state for shared MapContainer
|
||||||
val camState: CameraPositionState = rememberCameraPositionState {
|
val mapState = remember {
|
||||||
val lat = initialLat ?: myLat ?: 59.3293
|
MapState(
|
||||||
val lon = initialLon ?: myLon ?: 18.0686
|
initialCamera = CameraPosition(
|
||||||
position = CameraPosition.fromLatLngZoom(LatLng(lat, lon), 16f)
|
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) }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
LaunchedEffect(Unit) {
|
||||||
val fused = GmsLocationServices.getFusedLocationProviderClient(context)
|
val fused = GmsLocationServices.getFusedLocationProviderClient(context)
|
||||||
@@ -102,11 +113,27 @@ fun MapDialog(
|
|||||||
if (loc != null) {
|
if (loc != null) {
|
||||||
myLat = loc.latitude
|
myLat = loc.latitude
|
||||||
myLon = loc.longitude
|
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) {
|
LaunchedEffect(myLat, myLon, showKlotter, showWiki, wikiRadius, showDm, dmRadius, showFornsok, fornsokRadius) {
|
||||||
val lat = myLat; val lon = myLon
|
val lat = myLat; val lon = myLon
|
||||||
@@ -137,6 +164,59 @@ fun MapDialog(
|
|||||||
} else forn = emptyList()
|
} 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(
|
AlertDialog(
|
||||||
onDismissRequest = onClose,
|
onDismissRequest = onClose,
|
||||||
title = { Text("Karta") },
|
title = { Text("Karta") },
|
||||||
@@ -147,198 +227,68 @@ fun MapDialog(
|
|||||||
.heightIn(min = 320.dp, max = dialogHeight)
|
.heightIn(min = 320.dp, max = dialogHeight)
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
) {
|
) {
|
||||||
GoogleMap(
|
// Shared Map
|
||||||
modifier = Modifier.fillMaxSize(),
|
MapContainer(
|
||||||
cameraPositionState = camState,
|
state = mapState,
|
||||||
properties = MapProperties(
|
onMapClick = {
|
||||||
isMyLocationEnabled = hasFgLocationPerms(context),
|
// Clear selection
|
||||||
mapType = if (satellite) MapType.SATELLITE else MapType.NORMAL
|
selLat = null; selLon = null; selTitle = null; selUri = null; selId = null; selLayer = null
|
||||||
),
|
},
|
||||||
uiSettings = MapUiSettings(
|
onMarkerClick = { id ->
|
||||||
myLocationButtonEnabled = true,
|
fun setSel(lat: Double, lon: Double, title: String?, uri: String?, layer: String, itemId: String? = null) {
|
||||||
zoomControlsEnabled = false,
|
selLat = lat; selLon = lon; selTitle = title; selUri = uri; selLayer = layer; selId = itemId
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
}
|
when {
|
||||||
|
id.startsWith("klotter:") -> {
|
||||||
val lat = myLat; val lon = myLon
|
val nid = id.removePrefix("klotter:")
|
||||||
if (showKlotter) {
|
klotter.firstOrNull { it.i.toString() == nid }?.let { n ->
|
||||||
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 titleLine = n.t.substringBefore('\n').ifBlank { "Okänt klotter" }
|
||||||
val radius = Config.DEFAULT_KLOTTER_RADIUS_M
|
setSel(n.a, n.o, titleLine, null, "Klotter", nid)
|
||||||
val visited = isKnownNote(n.i.toLong())
|
onKlotterClick(nid, n.a, n.o, titleLine)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
) {
|
}
|
||||||
// Läslig bubblan med bakgrund
|
id.startsWith("wiki:") -> {
|
||||||
Card(
|
val pid = id.removePrefix("wiki:")
|
||||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
|
wiki.firstOrNull { it.pageId.toString() == pid }?.let { w ->
|
||||||
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
|
val url = "https://sv.m.wikipedia.org/?curid=${w.pageId}"
|
||||||
shape = RoundedCornerShape(8.dp)
|
setSel(w.lat, w.lon, w.title, url, "Wikipedia")
|
||||||
) {
|
onOpenUrl(w.title, url)
|
||||||
Column(Modifier.padding(8.dp)) {
|
}
|
||||||
Text("Klotter", style = MaterialTheme.typography.labelMedium)
|
}
|
||||||
Text(display, style = MaterialTheme.typography.bodyMedium)
|
id.startsWith("dm:") -> {
|
||||||
Text(
|
val mid = id.removePrefix("dm:")
|
||||||
"Läs mer",
|
dm.firstOrNull { (it.webUrl ?: it.url) == mid }?.let { dmi ->
|
||||||
style = MaterialTheme.typography.bodySmall,
|
val url = dmi.webUrl ?: dmi.url
|
||||||
modifier = Modifier.clickable {
|
setSel(dmi.lat, dmi.lon, dmi.title, url, "DigitaltMuseum")
|
||||||
onKlotterClick(n.i.toString(), n.a, n.o, display)
|
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())
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
// 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:
|
||||||
if (showWiki) {
|
showKlotter, showWiki, wikiRadius, showDm, dmRadius, showFornsok, fornsokRadius,
|
||||||
wiki.forEach { w ->
|
klotter, wiki, dm, forn
|
||||||
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") }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
confirmButton = {
|
confirmButton = {
|
||||||
Button(onClick = onClose) { Text("Stäng") }
|
Button(onClick = onClose) { Text("Stäng") }
|
||||||
@@ -372,3 +322,24 @@ private fun openWalkingDirections(ctx: Context, lat: Double?, lon: Double?, titl
|
|||||||
openInMaps(ctx, lat, lon, title)
|
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.runtime.*
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import com.google.android.gms.maps.CameraUpdateFactory
|
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 com.google.maps.android.compose.*
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.channels.BufferOverflow
|
import kotlinx.coroutines.channels.BufferOverflow
|
||||||
@@ -16,10 +18,23 @@ private class AndroidMapController(
|
|||||||
private val onSetFeatures: (List<MapFeature>) -> Unit,
|
private val onSetFeatures: (List<MapFeature>) -> Unit,
|
||||||
private val myLocationSetter: (Boolean) -> Unit,
|
private val myLocationSetter: (Boolean) -> Unit,
|
||||||
private val scope: CoroutineScope,
|
private val scope: CoroutineScope,
|
||||||
|
private val isReady: () -> Boolean,
|
||||||
) : MapController {
|
) : MapController {
|
||||||
private val _camera = MutableSharedFlow<CameraPosition>(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
|
private val _camera = MutableSharedFlow<CameraPosition>(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
|
||||||
override val cameraPosition = _camera.asSharedFlow()
|
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) {
|
override fun moveCamera(position: CameraPosition) {
|
||||||
|
if (!isReady()) {
|
||||||
|
pendingCamera = position
|
||||||
|
return
|
||||||
|
}
|
||||||
cameraState.move(
|
cameraState.move(
|
||||||
CameraUpdateFactory.newCameraPosition(
|
CameraUpdateFactory.newCameraPosition(
|
||||||
com.google.android.gms.maps.model.CameraPosition.fromLatLngZoom(
|
com.google.android.gms.maps.model.CameraPosition.fromLatLngZoom(
|
||||||
@@ -30,6 +45,10 @@ private class AndroidMapController(
|
|||||||
}
|
}
|
||||||
override fun animateCamera(position: CameraPosition) {
|
override fun animateCamera(position: CameraPosition) {
|
||||||
// Fire-and-forget on the main scope (rememberCoroutineScope()).
|
// Fire-and-forget on the main scope (rememberCoroutineScope()).
|
||||||
|
if (!isReady()) {
|
||||||
|
pendingCamera = position
|
||||||
|
return
|
||||||
|
}
|
||||||
scope.launch {
|
scope.launch {
|
||||||
cameraState.animate(
|
cameraState.animate(
|
||||||
CameraUpdateFactory.newCameraPosition(
|
CameraUpdateFactory.newCameraPosition(
|
||||||
@@ -63,13 +82,15 @@ actual fun MapContainer(
|
|||||||
|
|
||||||
var features by remember { mutableStateOf(state.features) }
|
var features by remember { mutableStateOf(state.features) }
|
||||||
var myLocationEnabled by remember { mutableStateOf(false) }
|
var myLocationEnabled by remember { mutableStateOf(false) }
|
||||||
|
var mapLoaded by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
val controller = remember(cameraState) {
|
val controller = remember(cameraState) {
|
||||||
AndroidMapController(
|
AndroidMapController(
|
||||||
cameraState = cameraState,
|
cameraState = cameraState,
|
||||||
onSetFeatures = { features = it },
|
onSetFeatures = { features = it },
|
||||||
myLocationSetter = { myLocationEnabled = it },
|
myLocationSetter = { myLocationEnabled = it },
|
||||||
scope = scope
|
scope = scope,
|
||||||
|
isReady = { mapLoaded }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,11 +107,21 @@ actual fun MapContainer(
|
|||||||
zoomControlsEnabled = state.ui.zoomControlsEnabled,
|
zoomControlsEnabled = state.ui.zoomControlsEnabled,
|
||||||
myLocationButtonEnabled = state.ui.myLocationButtonEnabled
|
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 = { latLng ->
|
||||||
onMapClick?.invoke(LatLng(latLng.latitude, latLng.longitude))
|
onMapClick?.invoke(LatLng(latLng.latitude, latLng.longitude))
|
||||||
},
|
},
|
||||||
onPOIClick = { /* optional */ },
|
onPOIClick = { /* optional */ },
|
||||||
|
onMapLoaded = {
|
||||||
|
mapLoaded = true
|
||||||
|
controller.flushPendingIfAny()
|
||||||
|
},
|
||||||
) {
|
) {
|
||||||
features.forEach { f ->
|
features.forEach { f ->
|
||||||
when (f) {
|
when (f) {
|
||||||
|
|||||||
@@ -17,10 +17,15 @@ sealed interface MapFeature {
|
|||||||
// Add Polyline/Polygon later if needed
|
// Add Polyline/Polygon later if needed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum class MapType {
|
||||||
|
NORMAL, SATELLITE
|
||||||
|
}
|
||||||
|
|
||||||
data class MapUiSettings(
|
data class MapUiSettings(
|
||||||
val compassEnabled: Boolean = true,
|
val compassEnabled: Boolean = true,
|
||||||
val zoomControlsEnabled: Boolean = false,
|
val zoomControlsEnabled: Boolean = false,
|
||||||
val myLocationButtonEnabled: Boolean = false,
|
val myLocationButtonEnabled: Boolean = false,
|
||||||
|
val mapType: MapType = MapType.NORMAL,
|
||||||
)
|
)
|
||||||
|
|
||||||
interface MapController {
|
interface MapController {
|
||||||
|
|||||||
@@ -70,6 +70,10 @@ actual fun MapContainer(
|
|||||||
mv.settings.zoomGestures = true
|
mv.settings.zoomGestures = true
|
||||||
mv.settings.scrollGestures = true
|
mv.settings.scrollGestures = true
|
||||||
mv.settings.myLocationButton = state.ui.myLocationButtonEnabled
|
mv.settings.myLocationButton = state.ui.myLocationButtonEnabled
|
||||||
|
mv.mapType = when (state.ui.mapType) {
|
||||||
|
MapType.SATELLITE -> kGMSTypeSatellite
|
||||||
|
MapType.NORMAL -> kGMSTypeNormal
|
||||||
|
}
|
||||||
mv.setDelegate(object : NSObject(), GMSMapViewDelegateProtocol {
|
mv.setDelegate(object : NSObject(), GMSMapViewDelegateProtocol {
|
||||||
|
|
||||||
|
|
||||||
@@ -98,6 +102,10 @@ actual fun MapContainer(
|
|||||||
// update UI toggles
|
// update UI toggles
|
||||||
map.settings.compassButton = state.ui.compassEnabled
|
map.settings.compassButton = state.ui.compassEnabled
|
||||||
map.settings.myLocationButton = state.ui.myLocationButtonEnabled
|
map.settings.myLocationButton = state.ui.myLocationButtonEnabled
|
||||||
|
map.mapType = when (state.ui.mapType) {
|
||||||
|
MapType.SATELLITE -> kGMSTypeSatellite
|
||||||
|
MapType.NORMAL -> kGMSTypeNormal
|
||||||
|
}
|
||||||
// update camera if changed (best-effort)
|
// update camera if changed (best-effort)
|
||||||
// (Optional: compare current camera to state.camera)
|
// (Optional: compare current camera to state.camera)
|
||||||
// re-apply features if list changed
|
// re-apply features if list changed
|
||||||
|
|||||||
Reference in New Issue
Block a user