@@ -14,10 +14,6 @@ import androidx.activity.compose.setContent
1414import androidx.compose.foundation.layout.Box
1515import androidx.compose.foundation.layout.fillMaxSize
1616import androidx.compose.foundation.layout.padding
17- import androidx.compose.material.icons.Icons
18- import androidx.compose.material.icons.filled.Refresh
19- import androidx.compose.material3.FloatingActionButton
20- import androidx.compose.material3.Icon
2117import androidx.compose.material3.MaterialTheme
2218import androidx.compose.material3.Surface
2319import androidx.compose.runtime.Composable
@@ -28,7 +24,6 @@ import androidx.compose.runtime.remember
2824import androidx.compose.runtime.setValue
2925import androidx.compose.ui.Alignment
3026import androidx.compose.ui.Modifier
31- import androidx.compose.ui.graphics.Color
3227import androidx.compose.ui.platform.LocalContext
3328import androidx.compose.ui.unit.dp
3429import androidx.compose.ui.viewinterop.AndroidView
@@ -39,14 +34,16 @@ import de.afarber.openmapview.LatLng
3934import de.afarber.openmapview.Marker
4035import de.afarber.openmapview.OnCameraMoveStartedListener
4136import de.afarber.openmapview.OpenMapView
37+ import kotlin.time.Duration.Companion.seconds
4238
4339/* *
44- * Main activity demonstrating OpenMapView marker management .
40+ * Main activity demonstrating OpenMapView marker navigation .
4541 *
4642 * This example showcases:
47- * - Adding markers with different colors at real Bochum locations
48- * - Marker click listener and info window display
49- * - Interactive marker management (add, remove, clear)
43+ * - Displaying markers with different colors at real Bochum locations
44+ * - Navigating between markers with prev/next buttons
45+ * - Toggling info windows on selected markers
46+ * - Camera animation when centering on markers
5047 * - Real-time marker count and selection tracking
5148 * - Camera state monitoring
5249 */
@@ -67,11 +64,11 @@ class MainActivity : ComponentActivity() {
6764}
6865
6966/* *
70- * Main composable screen containing the map and marker controls.
67+ * Main composable screen containing the map and marker navigation controls.
7168 *
7269 * Displays an OpenMapView with markers at notable Bochum locations,
7370 * a status toolbar showing marker count and selection state,
74- * and a marker toolbar for adding, removing, and clearing markers .
71+ * and a marker toolbar for navigating between markers and toggling info windows .
7572 */
7673@Composable
7774fun MapViewScreen () {
@@ -87,13 +84,12 @@ fun MapViewScreen() {
8784
8885 // State variables
8986 var mapView: OpenMapView ? by remember { mutableStateOf(null ) }
90- var markerCount by remember { mutableIntStateOf(initialMarkerData.size ) }
87+ var selectedIndex by remember { mutableIntStateOf(0 ) }
9188 var selectedMarker: Marker ? by remember { mutableStateOf(null ) }
9289 var cameraState by remember { mutableStateOf(" Idle" ) }
93- var addedMarkerCounter by remember { mutableIntStateOf(0 ) }
9490
9591 /* *
96- * Creates initial markers on the map.
92+ * Creates initial markers on the map and selects the first one .
9793 */
9894 fun createInitialMarkers (map : OpenMapView ) {
9995 initialMarkerData.forEach { data ->
@@ -106,8 +102,8 @@ fun MapViewScreen() {
106102 ),
107103 )
108104 }
109- markerCount = map.getMarkers().size
110- selectedMarker = null
105+ selectedIndex = 0
106+ selectedMarker = map.getMarkers().firstOrNull()
111107 }
112108
113109 Box (modifier = Modifier .fillMaxSize()) {
@@ -119,6 +115,7 @@ fun MapViewScreen() {
119115
120116 setCenter(initialLocation)
121117 setZoom(initialZoom)
118+ getUiSettings().infoWindowAutoDismiss = 10 .seconds
122119
123120 // Create initial markers
124121 createInitialMarkers(this )
@@ -141,6 +138,10 @@ fun MapViewScreen() {
141138 // Marker click listener - tracks selection and shows info window
142139 setOnMarkerClickListener { marker ->
143140 selectedMarker = marker
141+ val index = getMarkers().indexOf(marker)
142+ if (index >= 0 ) {
143+ selectedIndex = index
144+ }
144145 true // Consume the click event (info window will still show)
145146 }
146147
@@ -157,7 +158,7 @@ fun MapViewScreen() {
157158
158159 // Status overlay at top
159160 StatusToolbar (
160- markerCount = markerCount ,
161+ markerCount = mapView?.getMarkers()?.size ? : 0 ,
161162 selectedMarkerTitle = selectedMarker?.title,
162163 cameraState = cameraState,
163164 modifier = Modifier
@@ -167,68 +168,37 @@ fun MapViewScreen() {
167168
168169 // Marker toolbar at bottom
169170 MarkerToolbar (
170- onAddClick = {
171+ onPrevClick = {
171172 mapView?.apply {
172- val center = getCameraPosition().target
173- val hue = markerHues[addedMarkerCounter % markerHues.size]
174- addedMarkerCounter++
175- val newMarker = addMarker(
176- Marker (
177- position = center,
178- title = " Marker $addedMarkerCounter " ,
179- snippet = " Added at map center" ,
180- icon = BitmapDescriptorFactory .defaultMarker(hue),
181- ),
182- )
183- markerCount = getMarkers().size
184- selectedMarker = newMarker
173+ val markers = getMarkers()
174+ if (markers.isNotEmpty()) {
175+ selectedIndex = (selectedIndex - 1 + markers.size) % markers.size
176+ val marker = markers[selectedIndex]
177+ selectedMarker = marker
178+ animateCamera(CameraUpdateFactory .newLatLng(marker.position), 500 )
179+ }
185180 }
186181 },
187- onRemoveClick = {
182+ onNextClick = {
188183 mapView?.apply {
189- selectedMarker?.let { marker ->
190- removeMarker(marker)
191- markerCount = getMarkers().size
192- selectedMarker = null
184+ val markers = getMarkers()
185+ if (markers.isNotEmpty()) {
186+ selectedIndex = (selectedIndex + 1 ) % markers.size
187+ val marker = markers[selectedIndex]
188+ selectedMarker = marker
189+ animateCamera(CameraUpdateFactory .newLatLng(marker.position), 500 )
193190 }
194191 }
195192 },
196- onClearClick = {
197- mapView?.apply {
198- clearMarkers()
199- markerCount = 0
200- selectedMarker = null
201- addedMarkerCounter = 0
193+ onInfoClick = {
194+ selectedMarker?.let { marker ->
195+ marker.showInfoWindow()
196+ mapView?.animateCamera(CameraUpdateFactory .newLatLng(marker.position), 500 )
202197 }
203198 },
204199 modifier = Modifier
205200 .align(Alignment .BottomCenter )
206201 .padding(bottom = 16 .dp),
207202 )
208-
209- // Reset FAB at bottom-end
210- FloatingActionButton (
211- onClick = {
212- mapView?.apply {
213- clearMarkers()
214- createInitialMarkers(this )
215- addedMarkerCounter = 0
216- animateCamera(
217- CameraUpdateFactory .newLatLngZoom(initialLocation, initialZoom),
218- 500 ,
219- )
220- }
221- },
222- containerColor = OsmHighwayPink ,
223- contentColor = Color .Black ,
224- modifier = Modifier
225- .align(Alignment .BottomEnd )
226- .padding(16 .dp),
227- ) {
228- Icon (
229- imageVector = Icons .Default .Refresh ,
230- contentDescription = " Reset" ,
231- )
232- }
233203 }
234204}
0 commit comments