|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Alexander Farber |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + * |
| 5 | + * This file is part of the OpenMapView project (https://github.com/afarber/OpenMapView) |
| 6 | + */ |
| 7 | + |
| 8 | +package de.afarber.openmapview.example08circles |
| 9 | + |
| 10 | +import android.content.Intent |
| 11 | +import android.graphics.Color |
| 12 | +import android.net.Uri |
| 13 | +import android.os.Bundle |
| 14 | +import android.widget.Toast |
| 15 | +import androidx.activity.ComponentActivity |
| 16 | +import androidx.activity.compose.setContent |
| 17 | +import androidx.compose.foundation.layout.Box |
| 18 | +import androidx.compose.foundation.layout.Column |
| 19 | +import androidx.compose.foundation.layout.fillMaxSize |
| 20 | +import androidx.compose.foundation.layout.padding |
| 21 | +import androidx.compose.material.icons.Icons |
| 22 | +import androidx.compose.material.icons.filled.Add |
| 23 | +import androidx.compose.material.icons.filled.Clear |
| 24 | +import androidx.compose.material3.FloatingActionButton |
| 25 | +import androidx.compose.material3.Icon |
| 26 | +import androidx.compose.material3.MaterialTheme |
| 27 | +import androidx.compose.material3.Surface |
| 28 | +import androidx.compose.runtime.Composable |
| 29 | +import androidx.compose.runtime.LaunchedEffect |
| 30 | +import androidx.compose.runtime.getValue |
| 31 | +import androidx.compose.runtime.mutableStateOf |
| 32 | +import androidx.compose.runtime.remember |
| 33 | +import androidx.compose.runtime.setValue |
| 34 | +import androidx.compose.ui.Alignment |
| 35 | +import androidx.compose.ui.Modifier |
| 36 | +import androidx.compose.ui.platform.LocalContext |
| 37 | +import androidx.compose.ui.unit.dp |
| 38 | +import androidx.compose.ui.viewinterop.AndroidView |
| 39 | +import de.afarber.openmapview.CircleOptions |
| 40 | +import de.afarber.openmapview.LatLng |
| 41 | +import de.afarber.openmapview.OpenMapView |
| 42 | +import kotlin.random.Random |
| 43 | + |
| 44 | +class MainActivity : ComponentActivity() { |
| 45 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 46 | + super.onCreate(savedInstanceState) |
| 47 | + setContent { |
| 48 | + MaterialTheme { |
| 49 | + Surface( |
| 50 | + modifier = Modifier.fillMaxSize(), |
| 51 | + color = MaterialTheme.colorScheme.background, |
| 52 | + ) { |
| 53 | + MapViewScreen() |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +@Composable |
| 61 | +fun MapViewScreen() { |
| 62 | + val context = LocalContext.current |
| 63 | + val lifecycleOwner = androidx.lifecycle.compose.LocalLifecycleOwner.current |
| 64 | + var mapView by remember { mutableStateOf<OpenMapView?>(null) } |
| 65 | + var circleCount by remember { mutableStateOf(0) } |
| 66 | + |
| 67 | + val bochumCenter = LatLng(51.4661, 7.2491) |
| 68 | + |
| 69 | + // Show initial instruction toast |
| 70 | + LaunchedEffect(Unit) { |
| 71 | + Toast.makeText( |
| 72 | + context, |
| 73 | + "Click circles to see their properties.\nUse FABs to add random circles or clear all", |
| 74 | + Toast.LENGTH_LONG, |
| 75 | + ).show() |
| 76 | + } |
| 77 | + |
| 78 | + fun addDemoCircles() { |
| 79 | + mapView?.let { map -> |
| 80 | + // Circle 1: Small red circle with high z-index |
| 81 | + map.addCircle( |
| 82 | + CircleOptions() |
| 83 | + .center(bochumCenter) |
| 84 | + .radius(500f) |
| 85 | + .strokeColor(Color.RED) |
| 86 | + .strokeWidth(5f) |
| 87 | + .fillColor(Color.argb(64, 255, 0, 0)) |
| 88 | + .clickable(true) |
| 89 | + .zIndex(2f) |
| 90 | + .tag("Small Red Circle - 500m"), |
| 91 | + ) |
| 92 | + |
| 93 | + // Circle 2: Medium blue circle with mid z-index |
| 94 | + val offset1 = LatLng(bochumCenter.latitude + 0.01, bochumCenter.longitude + 0.01) |
| 95 | + map.addCircle( |
| 96 | + CircleOptions() |
| 97 | + .center(offset1) |
| 98 | + .radius(1000f) |
| 99 | + .strokeColor(Color.BLUE) |
| 100 | + .strokeWidth(8f) |
| 101 | + .fillColor(Color.argb(64, 0, 0, 255)) |
| 102 | + .clickable(true) |
| 103 | + .zIndex(1f) |
| 104 | + .tag("Medium Blue Circle - 1000m"), |
| 105 | + ) |
| 106 | + |
| 107 | + // Circle 3: Large green circle with low z-index |
| 108 | + val offset2 = LatLng(bochumCenter.latitude - 0.01, bochumCenter.longitude - 0.01) |
| 109 | + map.addCircle( |
| 110 | + CircleOptions() |
| 111 | + .center(offset2) |
| 112 | + .radius(1500f) |
| 113 | + .strokeColor(Color.GREEN) |
| 114 | + .strokeWidth(10f) |
| 115 | + .fillColor(Color.argb(64, 0, 255, 0)) |
| 116 | + .clickable(true) |
| 117 | + .zIndex(0f) |
| 118 | + .tag("Large Green Circle - 1500m"), |
| 119 | + ) |
| 120 | + |
| 121 | + circleCount += 3 |
| 122 | + Toast.makeText(context, "Added 3 demonstration circles", Toast.LENGTH_SHORT).show() |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + fun addRandomCircle() { |
| 127 | + mapView?.let { map -> |
| 128 | + val randomLat = bochumCenter.latitude + (Random.nextDouble() - 0.5) * 0.03 |
| 129 | + val randomLng = bochumCenter.longitude + (Random.nextDouble() - 0.5) * 0.06 |
| 130 | + val randomRadius = Random.nextInt(300, 1500).toFloat() |
| 131 | + val randomColor = Color.rgb(Random.nextInt(256), Random.nextInt(256), Random.nextInt(256)) |
| 132 | + |
| 133 | + map.addCircle( |
| 134 | + CircleOptions() |
| 135 | + .center(LatLng(randomLat, randomLng)) |
| 136 | + .radius(randomRadius) |
| 137 | + .strokeColor(randomColor) |
| 138 | + .strokeWidth(Random.nextInt(3, 12).toFloat()) |
| 139 | + .fillColor(Color.argb(64, Color.red(randomColor), Color.green(randomColor), Color.blue(randomColor))) |
| 140 | + .clickable(true) |
| 141 | + .zIndex(Random.nextFloat() * 5) |
| 142 | + .tag("Random Circle ${++circleCount} - ${randomRadius.toInt()}m"), |
| 143 | + ) |
| 144 | + |
| 145 | + Toast.makeText(context, "Added random circle", Toast.LENGTH_SHORT).show() |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + Box(modifier = Modifier.fillMaxSize()) { |
| 150 | + AndroidView( |
| 151 | + factory = { ctx -> |
| 152 | + OpenMapView(ctx).apply { |
| 153 | + mapView = this |
| 154 | + lifecycleOwner.lifecycle.addObserver(this) |
| 155 | + |
| 156 | + setCenter(bochumCenter) |
| 157 | + setZoom(12.0) |
| 158 | + |
| 159 | + // Add initial demonstration circles |
| 160 | + addDemoCircles() |
| 161 | + |
| 162 | + // Set circle click listener |
| 163 | + setOnCircleClickListener { circle -> |
| 164 | + val tagStr = circle.tag?.toString() ?: "Unknown Circle" |
| 165 | + val coordStr = "%.4f, %.4f".format(circle.center.latitude, circle.center.longitude) |
| 166 | + Toast.makeText( |
| 167 | + context, |
| 168 | + "$tagStr\nCenter: $coordStr\nZ-Index: ${circle.zIndex}", |
| 169 | + Toast.LENGTH_SHORT, |
| 170 | + ).show() |
| 171 | + } |
| 172 | + |
| 173 | + // Set attribution click listener |
| 174 | + setOnAttributionClickListener { |
| 175 | + val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.openstreetmap.org/copyright")) |
| 176 | + context.startActivity(intent) |
| 177 | + } |
| 178 | + } |
| 179 | + }, |
| 180 | + modifier = Modifier.fillMaxSize(), |
| 181 | + ) |
| 182 | + |
| 183 | + // Floating Action Buttons |
| 184 | + Column( |
| 185 | + modifier = |
| 186 | + Modifier |
| 187 | + .align(Alignment.BottomEnd) |
| 188 | + .padding(16.dp), |
| 189 | + horizontalAlignment = Alignment.End, |
| 190 | + ) { |
| 191 | + // Add random circle FAB |
| 192 | + FloatingActionButton( |
| 193 | + onClick = { addRandomCircle() }, |
| 194 | + modifier = Modifier.padding(bottom = 16.dp), |
| 195 | + ) { |
| 196 | + Icon( |
| 197 | + imageVector = Icons.Default.Add, |
| 198 | + contentDescription = "Add random circle", |
| 199 | + ) |
| 200 | + } |
| 201 | + |
| 202 | + // Clear all circles FAB |
| 203 | + FloatingActionButton( |
| 204 | + onClick = { |
| 205 | + mapView?.clear() |
| 206 | + circleCount = 0 |
| 207 | + Toast.makeText( |
| 208 | + context, |
| 209 | + "All circles cleared", |
| 210 | + Toast.LENGTH_SHORT, |
| 211 | + ).show() |
| 212 | + }, |
| 213 | + ) { |
| 214 | + Icon( |
| 215 | + imageVector = Icons.Default.Clear, |
| 216 | + contentDescription = "Clear all circles", |
| 217 | + ) |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments