@@ -10,14 +10,28 @@ package de.afarber.openmapview.example02zoom
1010import android.os.Bundle
1111import androidx.activity.ComponentActivity
1212import androidx.activity.compose.setContent
13+ import androidx.compose.foundation.layout.Box
14+ import androidx.compose.foundation.layout.Column
15+ import androidx.compose.foundation.layout.Spacer
1316import androidx.compose.foundation.layout.fillMaxSize
17+ import androidx.compose.foundation.layout.height
18+ import androidx.compose.foundation.layout.padding
19+ import androidx.compose.material3.FloatingActionButton
1420import androidx.compose.material3.MaterialTheme
1521import androidx.compose.material3.Surface
22+ import androidx.compose.material3.Text
1623import androidx.compose.runtime.Composable
24+ import androidx.compose.runtime.getValue
25+ import androidx.compose.runtime.mutableStateOf
26+ import androidx.compose.runtime.remember
27+ import androidx.compose.runtime.setValue
28+ import androidx.compose.ui.Alignment
1729import androidx.compose.ui.Modifier
30+ import androidx.compose.ui.unit.dp
1831import androidx.compose.ui.viewinterop.AndroidView
1932import de.afarber.openmapview.LatLng
2033import de.afarber.openmapview.OpenMapView
34+ import kotlin.math.roundToInt
2135
2236class MainActivity : ComponentActivity () {
2337 override fun onCreate (savedInstanceState : Bundle ? ) {
@@ -37,14 +51,68 @@ class MainActivity : ComponentActivity() {
3751
3852@Composable
3953fun MapViewScreen () {
40- // TODO: Add zoom gesture support
41- AndroidView (
42- factory = { context ->
43- OpenMapView (context).apply {
44- setCenter(LatLng (51.4661 , 7.2491 )) // Bochum, Germany
45- setZoom(14.0 )
54+ var zoomLevel by remember { mutableStateOf(14.0 ) }
55+ var mapView: OpenMapView ? by remember { mutableStateOf(null ) }
56+
57+ Box (modifier = Modifier .fillMaxSize()) {
58+ AndroidView (
59+ factory = { context ->
60+ OpenMapView (context).apply {
61+ setCenter(LatLng (51.4661 , 7.2491 )) // Bochum, Germany
62+ setZoom(14.0 )
63+ mapView = this
64+ }
65+ },
66+ modifier = Modifier .fillMaxSize(),
67+ )
68+
69+ // Zoom controls overlay
70+ Column (
71+ modifier = Modifier
72+ .align(Alignment .BottomEnd )
73+ .padding(16 .dp),
74+ ) {
75+ // Zoom level display
76+ Surface (
77+ color = MaterialTheme .colorScheme.surface.copy(alpha = 0.9f ),
78+ shape = MaterialTheme .shapes.small,
79+ ) {
80+ Text (
81+ text = " Zoom: ${zoomLevel.roundToInt()} " ,
82+ modifier = Modifier .padding(12 .dp),
83+ style = MaterialTheme .typography.bodyMedium,
84+ )
85+ }
86+
87+ Spacer (modifier = Modifier .height(8 .dp))
88+
89+ // Zoom in button
90+ FloatingActionButton (
91+ onClick = {
92+ mapView?.let {
93+ val newZoom = (it.getZoom() + 1.0 ).coerceAtMost(19.0 )
94+ it.setZoom(newZoom)
95+ zoomLevel = newZoom
96+ }
97+ },
98+ ) {
99+ Text (" +" , style = MaterialTheme .typography.headlineMedium)
46100 }
47- },
48- modifier = Modifier .fillMaxSize(),
49- )
101+
102+ Spacer (modifier = Modifier .height(8 .dp))
103+
104+ // Zoom out button
105+ FloatingActionButton (
106+ onClick = {
107+ mapView?.let {
108+ val newZoom = (it.getZoom() - 1.0 ).coerceAtLeast(2.0 )
109+ it.setZoom(newZoom)
110+ zoomLevel = newZoom
111+ }
112+ },
113+ ) {
114+ Text (" -" , style = MaterialTheme .typography.headlineMedium)
115+ }
116+ }
117+ }
50118}
0 commit comments