mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
feat(map): Persist Google Maps camera position (#3605)
Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
parent
78a10118a0
commit
6e06d27701
10 changed files with 184 additions and 37 deletions
|
|
@ -86,7 +86,6 @@ import com.google.maps.android.compose.MarkerComposable
|
|||
import com.google.maps.android.compose.MarkerInfoWindowComposable
|
||||
import com.google.maps.android.compose.Polyline
|
||||
import com.google.maps.android.compose.TileOverlay
|
||||
import com.google.maps.android.compose.rememberCameraPositionState
|
||||
import com.google.maps.android.compose.rememberUpdatedMarkerState
|
||||
import com.google.maps.android.compose.widgets.ScaleBar
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
|
@ -162,15 +161,13 @@ fun MapView(
|
|||
var mapTypeMenuExpanded by remember { mutableStateOf(false) }
|
||||
var showCustomTileManagerSheet by remember { mutableStateOf(false) }
|
||||
|
||||
val cameraPositionState = rememberCameraPositionState {
|
||||
position =
|
||||
CameraPosition.fromLatLngZoom(
|
||||
LatLng(
|
||||
ourNodeInfo?.position?.latitudeI?.times(DEG_D) ?: 0.0,
|
||||
ourNodeInfo?.position?.longitudeI?.times(DEG_D) ?: 0.0,
|
||||
),
|
||||
7f,
|
||||
)
|
||||
val cameraPositionState = mapViewModel.cameraPositionState
|
||||
|
||||
// Save camera position when it stops moving
|
||||
LaunchedEffect(cameraPositionState.isMoving) {
|
||||
if (!cameraPositionState.isMoving) {
|
||||
mapViewModel.saveCameraPosition(cameraPositionState.position)
|
||||
}
|
||||
}
|
||||
|
||||
// Location tracking functionality
|
||||
|
|
@ -221,6 +218,7 @@ fun MapView(
|
|||
.build()
|
||||
|
||||
try {
|
||||
@Suppress("MissingPermission")
|
||||
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null)
|
||||
Timber.d("Started location tracking")
|
||||
} catch (e: SecurityException) {
|
||||
|
|
@ -351,31 +349,6 @@ fun MapView(
|
|||
editingWaypoint = newWaypoint
|
||||
}
|
||||
},
|
||||
onMapLoaded = {
|
||||
val pointsToBound: List<LatLng> =
|
||||
when {
|
||||
!nodeTracks.isNullOrEmpty() -> nodeTracks.map { it.toLatLng() }
|
||||
|
||||
allNodes.isNotEmpty() || displayableWaypoints.isNotEmpty() ->
|
||||
allNodes.mapNotNull { it.toLatLng() } + displayableWaypoints.map { it.toLatLng() }
|
||||
|
||||
else -> emptyList()
|
||||
}
|
||||
|
||||
if (pointsToBound.isNotEmpty()) {
|
||||
val bounds = LatLngBounds.builder().apply { pointsToBound.forEach(::include) }.build()
|
||||
|
||||
val padding = if (!pointsToBound.isEmpty()) 100 else 48
|
||||
|
||||
try {
|
||||
coroutineScope.launch {
|
||||
cameraPositionState.animate(CameraUpdateFactory.newLatLngBounds(bounds, padding))
|
||||
}
|
||||
} catch (e: IllegalStateException) {
|
||||
Timber.w("MapView Could not animate to bounds: ${e.message}")
|
||||
}
|
||||
}
|
||||
},
|
||||
) {
|
||||
key(currentCustomTileProviderUrl) {
|
||||
currentCustomTileProviderUrl?.let { url ->
|
||||
|
|
@ -706,7 +679,7 @@ private fun speedFromPosition(position: Position, displayUnits: DisplayUnits): S
|
|||
return speedText
|
||||
}
|
||||
|
||||
private fun Position.toLatLng(): LatLng = LatLng(this.latitudeI * DEG_D, this.longitudeI * DEG_D)
|
||||
internal fun Position.toLatLng(): LatLng = LatLng(this.latitudeI * DEG_D, this.longitudeI * DEG_D)
|
||||
|
||||
private fun Node.toLatLng(): LatLng? = this.position.toLatLng()
|
||||
|
||||
|
|
|
|||
|
|
@ -22,8 +22,11 @@ import android.net.Uri
|
|||
import androidx.core.net.toFile
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.google.android.gms.maps.GoogleMap
|
||||
import com.google.android.gms.maps.model.CameraPosition
|
||||
import com.google.android.gms.maps.model.LatLng
|
||||
import com.google.android.gms.maps.model.TileProvider
|
||||
import com.google.android.gms.maps.model.UrlTileProvider
|
||||
import com.google.maps.android.compose.CameraPositionState
|
||||
import com.google.maps.android.compose.MapType
|
||||
import com.google.maps.android.data.geojson.GeoJsonLayer
|
||||
import com.google.maps.android.data.kml.KmlLayer
|
||||
|
|
@ -90,6 +93,24 @@ constructor(
|
|||
uiPreferencesDataSource: UiPreferencesDataSource,
|
||||
) : BaseMapViewModel(mapPrefs, nodeRepository, packetRepository, serviceRepository) {
|
||||
|
||||
private val targetLatLng =
|
||||
googleMapsPrefs.cameraTargetLat
|
||||
.takeIf { it != 0.0 }
|
||||
?.let { lat -> googleMapsPrefs.cameraTargetLng.takeIf { it != 0.0 }?.let { lng -> LatLng(lat, lng) } }
|
||||
?: ourNodeInfo.value?.position?.toLatLng()
|
||||
?: LatLng(0.0, 0.0)
|
||||
|
||||
val cameraPositionState =
|
||||
CameraPositionState(
|
||||
position =
|
||||
CameraPosition(
|
||||
targetLatLng,
|
||||
googleMapsPrefs.cameraZoom,
|
||||
googleMapsPrefs.cameraTilt,
|
||||
googleMapsPrefs.cameraBearing,
|
||||
),
|
||||
)
|
||||
|
||||
val theme: StateFlow<Int> = uiPreferencesDataSource.theme
|
||||
|
||||
private val _errorFlow = MutableSharedFlow<String>()
|
||||
|
|
@ -238,6 +259,16 @@ constructor(
|
|||
loadPersistedLayers()
|
||||
}
|
||||
|
||||
fun saveCameraPosition(cameraPosition: CameraPosition) {
|
||||
viewModelScope.launch {
|
||||
googleMapsPrefs.cameraTargetLat = cameraPosition.target.latitude
|
||||
googleMapsPrefs.cameraTargetLng = cameraPosition.target.longitude
|
||||
googleMapsPrefs.cameraZoom = cameraPosition.zoom
|
||||
googleMapsPrefs.cameraTilt = cameraPosition.tilt
|
||||
googleMapsPrefs.cameraBearing = cameraPosition.bearing
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadPersistedMapType() {
|
||||
val savedCustomUrl = googleMapsPrefs.selectedCustomTileUrl
|
||||
if (savedCustomUrl != null) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue