Skip to content

Commit 9d7d7cd

Browse files
committed
Just navigate between the markers instead of adding/removing them
1 parent b552d97 commit 9d7d7cd

5 files changed

Lines changed: 62 additions & 106 deletions

File tree

examples/Example01Pan/src/main/kotlin/de/afarber/openmapview/example01pan/ArrowToolbar.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fun ArrowToolbar(
9090
modifier = Modifier.size(56.dp),
9191
shape = RoundedCornerShape(topEnd = ToolbarCornerRadius, bottomEnd = ToolbarCornerRadius),
9292
colors = IconButtonDefaults.filledIconButtonColors(
93-
containerColor = OsmParkGreen,
93+
containerColor = OsmHighwayPink,
9494
contentColor = Color.Black,
9595
),
9696
) {

examples/Example02Zoom/src/main/kotlin/de/afarber/openmapview/example02zoom/ZoomToolbar.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import androidx.compose.foundation.layout.Row
1111
import androidx.compose.foundation.layout.size
1212
import androidx.compose.foundation.shape.RoundedCornerShape
1313
import androidx.compose.material.icons.Icons
14-
import androidx.compose.material.icons.filled.Add
15-
import androidx.compose.material.icons.filled.Remove
14+
import androidx.compose.material.icons.filled.ZoomIn
15+
import androidx.compose.material.icons.filled.ZoomOut
1616
import androidx.compose.material3.FilledIconButton
1717
import androidx.compose.material3.Icon
1818
import androidx.compose.material3.IconButtonDefaults
@@ -54,7 +54,7 @@ fun ZoomToolbar(
5454
contentColor = Color.Black,
5555
),
5656
) {
57-
Icon(Icons.Default.Add, contentDescription = "Zoom In")
57+
Icon(Icons.Default.ZoomIn, contentDescription = "Zoom In")
5858
}
5959
FilledIconButton(
6060
onClick = onZoomOutClick,
@@ -65,7 +65,7 @@ fun ZoomToolbar(
6565
contentColor = Color.Black,
6666
),
6767
) {
68-
Icon(Icons.Default.Remove, contentDescription = "Zoom Out")
68+
Icon(Icons.Default.ZoomOut, contentDescription = "Zoom Out")
6969
}
7070
}
7171
}

examples/Example03Markers/src/main/kotlin/de/afarber/openmapview/example03markers/MainActivity.kt

Lines changed: 37 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ import androidx.activity.compose.setContent
1414
import androidx.compose.foundation.layout.Box
1515
import androidx.compose.foundation.layout.fillMaxSize
1616
import 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
2117
import androidx.compose.material3.MaterialTheme
2218
import androidx.compose.material3.Surface
2319
import androidx.compose.runtime.Composable
@@ -28,7 +24,6 @@ import androidx.compose.runtime.remember
2824
import androidx.compose.runtime.setValue
2925
import androidx.compose.ui.Alignment
3026
import androidx.compose.ui.Modifier
31-
import androidx.compose.ui.graphics.Color
3227
import androidx.compose.ui.platform.LocalContext
3328
import androidx.compose.ui.unit.dp
3429
import androidx.compose.ui.viewinterop.AndroidView
@@ -39,14 +34,16 @@ import de.afarber.openmapview.LatLng
3934
import de.afarber.openmapview.Marker
4035
import de.afarber.openmapview.OnCameraMoveStartedListener
4136
import 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
7774
fun 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
}

examples/Example03Markers/src/main/kotlin/de/afarber/openmapview/example03markers/MarkerData.kt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,3 @@ val initialMarkerData = listOf(
6464
hue = BitmapDescriptorFactory.HUE_CYAN,
6565
),
6666
)
67-
68-
/** Color hues for dynamically added markers, cycling through available colors. */
69-
val markerHues = listOf(
70-
BitmapDescriptorFactory.HUE_RED,
71-
BitmapDescriptorFactory.HUE_BLUE,
72-
BitmapDescriptorFactory.HUE_GREEN,
73-
BitmapDescriptorFactory.HUE_ORANGE,
74-
BitmapDescriptorFactory.HUE_MAGENTA,
75-
BitmapDescriptorFactory.HUE_CYAN,
76-
BitmapDescriptorFactory.HUE_YELLOW,
77-
BitmapDescriptorFactory.HUE_VIOLET,
78-
BitmapDescriptorFactory.HUE_ROSE,
79-
BitmapDescriptorFactory.HUE_AZURE,
80-
)

examples/Example03Markers/src/main/kotlin/de/afarber/openmapview/example03markers/MarkerToolbar.kt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import androidx.compose.foundation.layout.Row
1111
import androidx.compose.foundation.layout.size
1212
import androidx.compose.foundation.shape.RoundedCornerShape
1313
import androidx.compose.material.icons.Icons
14-
import androidx.compose.material.icons.filled.Add
15-
import androidx.compose.material.icons.filled.Delete
16-
import androidx.compose.material.icons.filled.Remove
14+
import androidx.compose.material.icons.filled.KeyboardDoubleArrowLeft
15+
import androidx.compose.material.icons.filled.KeyboardDoubleArrowRight
16+
import androidx.compose.material.icons.filled.LocationOn
1717
import androidx.compose.material3.FilledIconButton
1818
import androidx.compose.material3.Icon
1919
import androidx.compose.material3.IconButtonDefaults
@@ -25,23 +25,23 @@ import androidx.compose.ui.graphics.Color
2525
import androidx.compose.ui.unit.dp
2626

2727
/**
28-
* A horizontal toolbar with buttons to add, remove, and clear markers.
28+
* A horizontal toolbar with buttons to navigate markers and toggle info windows.
2929
*
3030
* Uses OSM-inspired colors for visual consistency:
31-
* - Add button: OsmParkGreen
32-
* - Remove button: OsmHighwayPink
33-
* - Clear button: OsmWaterBlue
31+
* - Previous button: OsmParkGreen
32+
* - Next button: OsmParkGreen
33+
* - Info button: OsmWaterBlue
3434
*
35-
* @param onAddClick Callback invoked when the add marker button is clicked.
36-
* @param onRemoveClick Callback invoked when the remove marker button is clicked.
37-
* @param onClearClick Callback invoked when the clear all markers button is clicked.
35+
* @param onPrevClick Callback invoked when the previous marker button is clicked.
36+
* @param onNextClick Callback invoked when the next marker button is clicked.
37+
* @param onInfoClick Callback invoked when the info window toggle button is clicked.
3838
* @param modifier Modifier to be applied to the toolbar.
3939
*/
4040
@Composable
4141
fun MarkerToolbar(
42-
onAddClick: () -> Unit,
43-
onRemoveClick: () -> Unit,
44-
onClearClick: () -> Unit,
42+
onPrevClick: () -> Unit,
43+
onNextClick: () -> Unit,
44+
onInfoClick: () -> Unit,
4545
modifier: Modifier = Modifier,
4646
) {
4747
Surface(
@@ -52,37 +52,37 @@ fun MarkerToolbar(
5252
) {
5353
Row {
5454
FilledIconButton(
55-
onClick = onAddClick,
55+
onClick = onPrevClick,
5656
modifier = Modifier.size(56.dp),
5757
shape = RoundedCornerShape(topStart = ToolbarCornerRadius, bottomStart = ToolbarCornerRadius),
5858
colors = IconButtonDefaults.filledIconButtonColors(
5959
containerColor = OsmParkGreen,
6060
contentColor = Color.Black,
6161
),
6262
) {
63-
Icon(Icons.Default.Add, contentDescription = "Add Marker")
63+
Icon(Icons.Default.KeyboardDoubleArrowLeft, contentDescription = "Previous Marker")
6464
}
6565
FilledIconButton(
66-
onClick = onRemoveClick,
66+
onClick = onNextClick,
6767
modifier = Modifier.size(56.dp),
6868
shape = RoundedCornerShape(0.dp),
6969
colors = IconButtonDefaults.filledIconButtonColors(
70-
containerColor = OsmHighwayPink,
70+
containerColor = OsmParkGreen,
7171
contentColor = Color.Black,
7272
),
7373
) {
74-
Icon(Icons.Default.Remove, contentDescription = "Remove Marker")
74+
Icon(Icons.Default.KeyboardDoubleArrowRight, contentDescription = "Next Marker")
7575
}
7676
FilledIconButton(
77-
onClick = onClearClick,
77+
onClick = onInfoClick,
7878
modifier = Modifier.size(56.dp),
7979
shape = RoundedCornerShape(topEnd = ToolbarCornerRadius, bottomEnd = ToolbarCornerRadius),
8080
colors = IconButtonDefaults.filledIconButtonColors(
8181
containerColor = OsmWaterBlue,
8282
contentColor = Color.Black,
8383
),
8484
) {
85-
Icon(Icons.Default.Delete, contentDescription = "Clear All Markers")
85+
Icon(Icons.Default.LocationOn, contentDescription = "Show Marker Info")
8686
}
8787
}
8888
}

0 commit comments

Comments
 (0)