|
| 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.example06clicks |
| 9 | + |
| 10 | +import android.content.Intent |
| 11 | +import android.net.Uri |
| 12 | +import android.os.Bundle |
| 13 | +import android.widget.Toast |
| 14 | +import androidx.activity.ComponentActivity |
| 15 | +import androidx.activity.compose.setContent |
| 16 | +import androidx.compose.foundation.layout.fillMaxSize |
| 17 | +import androidx.compose.material3.MaterialTheme |
| 18 | +import androidx.compose.material3.Surface |
| 19 | +import androidx.compose.runtime.Composable |
| 20 | +import androidx.compose.runtime.LaunchedEffect |
| 21 | +import androidx.compose.ui.Modifier |
| 22 | +import androidx.compose.ui.platform.LocalContext |
| 23 | +import androidx.compose.ui.viewinterop.AndroidView |
| 24 | +import de.afarber.openmapview.LatLng |
| 25 | +import de.afarber.openmapview.OnMapClickListener |
| 26 | +import de.afarber.openmapview.OnMapLongClickListener |
| 27 | +import de.afarber.openmapview.OpenMapView |
| 28 | + |
| 29 | +class MainActivity : ComponentActivity() { |
| 30 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 31 | + super.onCreate(savedInstanceState) |
| 32 | + setContent { |
| 33 | + MaterialTheme { |
| 34 | + Surface( |
| 35 | + modifier = Modifier.fillMaxSize(), |
| 36 | + color = MaterialTheme.colorScheme.background, |
| 37 | + ) { |
| 38 | + MapViewScreen() |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +@Composable |
| 46 | +fun MapViewScreen() { |
| 47 | + val context = LocalContext.current |
| 48 | + val lifecycleOwner = androidx.lifecycle.compose.LocalLifecycleOwner.current |
| 49 | + |
| 50 | + // Show initial instruction toast |
| 51 | + LaunchedEffect(Unit) { |
| 52 | + Toast.makeText( |
| 53 | + context, |
| 54 | + "Try dragging the map around.\nTry short and long clicks on the map", |
| 55 | + Toast.LENGTH_LONG, |
| 56 | + ).show() |
| 57 | + } |
| 58 | + |
| 59 | + AndroidView( |
| 60 | + factory = { ctx -> |
| 61 | + OpenMapView(ctx).apply { |
| 62 | + // Register lifecycle observer for proper cleanup |
| 63 | + lifecycleOwner.lifecycle.addObserver(this) |
| 64 | + |
| 65 | + setCenter(LatLng(51.4661, 7.2491)) // Bochum, Germany |
| 66 | + setZoom(14.0) |
| 67 | + |
| 68 | + // Set map click listener |
| 69 | + setOnMapClickListener( |
| 70 | + OnMapClickListener { latLng -> |
| 71 | + val coordString = "%.4f, %.4f".format(latLng.latitude, latLng.longitude) |
| 72 | + Toast.makeText( |
| 73 | + context, |
| 74 | + "Clicked at: $coordString", |
| 75 | + Toast.LENGTH_SHORT, |
| 76 | + ).show() |
| 77 | + }, |
| 78 | + ) |
| 79 | + |
| 80 | + // Set map long-click listener |
| 81 | + setOnMapLongClickListener( |
| 82 | + OnMapLongClickListener { latLng -> |
| 83 | + val coordString = "%.4f, %.4f".format(latLng.latitude, latLng.longitude) |
| 84 | + Toast.makeText( |
| 85 | + context, |
| 86 | + "Long-clicked at: $coordString", |
| 87 | + Toast.LENGTH_SHORT, |
| 88 | + ).show() |
| 89 | + }, |
| 90 | + ) |
| 91 | + |
| 92 | + // Set attribution click listener to open OSM copyright page |
| 93 | + setOnAttributionClickListener { |
| 94 | + val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.openstreetmap.org/copyright")) |
| 95 | + context.startActivity(intent) |
| 96 | + } |
| 97 | + } |
| 98 | + }, |
| 99 | + modifier = Modifier.fillMaxSize(), |
| 100 | + ) |
| 101 | +} |
0 commit comments