diff --git a/app/src/google/java/com/geeksville/mesh/ui/map/MapView.kt b/app/src/google/java/com/geeksville/mesh/ui/map/MapView.kt index 69beee3b6..239acad2d 100644 --- a/app/src/google/java/com/geeksville/mesh/ui/map/MapView.kt +++ b/app/src/google/java/com/geeksville/mesh/ui/map/MapView.kt @@ -57,7 +57,6 @@ import androidx.compose.runtime.key import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope -import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -105,7 +104,6 @@ import com.google.android.gms.maps.model.LatLng import com.google.android.gms.maps.model.LatLngBounds import com.google.android.gms.maps.model.RoundCap import com.google.maps.android.clustering.ClusterItem -import com.google.maps.android.compose.CameraMoveStartedReason import com.google.maps.android.compose.ComposeMapColorScheme import com.google.maps.android.compose.GoogleMap import com.google.maps.android.compose.MapEffect @@ -253,37 +251,6 @@ fun MapView( val waypoints by mapViewModel.waypoints.collectAsStateWithLifecycle(emptyMap()) val displayableWaypoints = waypoints.values.mapNotNull { it.data.waypoint } - var hasZoomed by rememberSaveable { mutableStateOf(false) } - - LaunchedEffect(allNodes, displayableWaypoints, nodeTrack) { - if ((hasZoomed) || cameraPositionState.cameraMoveStartedReason != CameraMoveStartedReason.NO_MOVEMENT_YET) { - if (!hasZoomed) hasZoomed = true - return@LaunchedEffect - } - - val pointsToBound: List = - when { - !nodeTrack.isNullOrEmpty() -> nodeTrack.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 { - cameraPositionState.animate(CameraUpdateFactory.newLatLngBounds(bounds, padding)) - hasZoomed = true - } catch (e: IllegalStateException) { - warn("MapView Could not animate to bounds: ${e.message}") - } - } - } val filteredNodes = if (mapFilterState.onlyFavorites) { allNodes.filter { it.isFavorite || it.num == ourNodeInfo?.num } @@ -368,6 +335,36 @@ fun MapView( editingWaypoint = newWaypoint } }, + onMapLoaded = { + if ( + savedCameraPosition?.targetLat == defaultLatLng.latitude && + savedCameraPosition?.targetLng == defaultLatLng.longitude + ) { + val pointsToBound: List = + when { + !nodeTrack.isNullOrEmpty() -> nodeTrack.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) { + warn("MapView Could not animate to bounds: ${e.message}") + } + } + } + }, ) { key(currentCustomTileProviderUrl) { currentCustomTileProviderUrl?.let { url ->