Skip to content

Commit 62203e8

Browse files
committed
add calloutView and tappedPoint as MapViewSTate property
1 parent eae6aa9 commit 62203e8

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

show-callout/src/main/java/com/esri/arcgismaps/sample/showcallout/components/ComposeMapView.kt

+10-15
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ import androidx.compose.ui.platform.LocalContext
2828
import androidx.compose.ui.platform.LocalLifecycleOwner
2929
import androidx.compose.ui.viewinterop.AndroidView
3030
import androidx.lifecycle.LifecycleOwner
31+
import androidx.lifecycle.lifecycleScope
3132
import com.arcgismaps.geometry.GeometryEngine
3233
import com.arcgismaps.geometry.Point
3334
import com.arcgismaps.geometry.SpatialReference
3435
import com.arcgismaps.mapping.view.MapView
36+
import com.arcgismaps.mapping.view.SingleTapConfirmedEvent
3537
import com.esri.arcgismaps.sample.showcallout.R
3638
import kotlinx.coroutines.launch
3739

@@ -42,7 +44,7 @@ import kotlinx.coroutines.launch
4244
fun ComposeMapView(
4345
modifier: Modifier = Modifier,
4446
mapViewModel: MapViewModel,
45-
application: Application
47+
onSingleTap: (SingleTapConfirmedEvent) -> Unit = {}
4648
) {
4749
// get an instance of the current lifecycle owner
4850
val lifecycleOwner = LocalLifecycleOwner.current
@@ -60,6 +62,12 @@ fun ComposeMapView(
6062
mapView.apply {
6163
map = mapViewState.arcGISMap
6264
setViewpoint(mapViewState.viewpoint)
65+
// show callout at the tapped location using the set View
66+
mapView.callout.show(mapViewState.calloutContent, mapViewState.latLongPoint)
67+
lifecycleOwner.lifecycleScope.launch {
68+
// center the map on the tapped location
69+
setViewpointCenter(mapViewState.latLongPoint)
70+
}
6371
}
6472
}
6573
)
@@ -68,20 +76,7 @@ fun ComposeMapView(
6876
LaunchedEffect(Unit) {
6977
launch {
7078
mapView.onSingleTapConfirmed.collect {
71-
// get map point from the Single tap event
72-
it.mapPoint?.let { mapPoint ->
73-
// convert the point to WGS84 for obtaining lat/lon format
74-
val wgs84Point =
75-
GeometryEngine.projectOrNull(mapPoint, SpatialReference.wgs84()) as Point
76-
// create a textview for the callout
77-
val calloutView = TextView(application).apply {
78-
text = application.getString(R.string.callout_text, wgs84Point.y, wgs84Point.x)
79-
}
80-
// show callout at the tapped location using the set View
81-
mapView.callout.show(calloutView, wgs84Point)
82-
// center the map on the tapped location
83-
mapView.setViewpointCenter(mapPoint)
84-
}
79+
onSingleTap(it)
8580
}
8681
}
8782
}

show-callout/src/main/java/com/esri/arcgismaps/sample/showcallout/components/MapViewModel.kt

+28-6
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,43 @@
1717
package com.esri.arcgismaps.sample.showcallout.components
1818

1919
import android.app.Application
20+
import android.widget.TextView
21+
import androidx.compose.runtime.getValue
22+
import androidx.compose.runtime.mutableStateOf
23+
import androidx.compose.runtime.setValue
2024
import androidx.lifecycle.AndroidViewModel
25+
import com.arcgismaps.geometry.GeometryEngine
26+
import com.arcgismaps.geometry.Point
27+
import com.arcgismaps.geometry.SpatialReference
2128
import com.arcgismaps.mapping.ArcGISMap
2229
import com.arcgismaps.mapping.BasemapStyle
2330
import com.arcgismaps.mapping.Viewpoint
31+
import com.esri.arcgismaps.sample.showcallout.R
2432
import kotlinx.coroutines.flow.MutableStateFlow
2533

26-
class MapViewModel(application: Application) : AndroidViewModel(application) {
34+
class MapViewModel(private val application: Application) : AndroidViewModel(application) {
2735
// set the MapView mutable stateflow
28-
val mapViewState = MutableStateFlow(MapViewState())
29-
}
36+
val mapViewState = MutableStateFlow(MapViewState(application))
37+
38+
fun onMapTapped(mapPoint: Point?) {
39+
// get map point from the Single tap event
40+
mapPoint?.let { mapPoint ->
41+
// convert the point to WGS84 for obtaining lat/lon format
42+
mapViewState.value.latLongPoint =
43+
GeometryEngine.projectOrNull(mapPoint, SpatialReference.wgs84()) as Point
44+
// create a textview for the callout
45+
mapViewState.value.calloutContent.text = application.getString(R.string.callout_text, mapViewState.value.latLongPoint.y, mapViewState.value.latLongPoint.x)
46+
}
47+
}
48+
}
49+
3050

3151
/**
3252
* Data class that represents the MapView state
3353
*/
34-
data class MapViewState(
35-
var arcGISMap: ArcGISMap = ArcGISMap(BasemapStyle.ArcGISNavigationNight),
54+
data class MapViewState(val application: Application) {
55+
var arcGISMap: ArcGISMap = ArcGISMap(BasemapStyle.ArcGISNavigationNight)
3656
var viewpoint: Viewpoint = Viewpoint(34.056295, -117.195800, 1000000.0)
37-
)
57+
var calloutContent: TextView by mutableStateOf(TextView(application))
58+
var latLongPoint: Point by mutableStateOf(Point(0.0, 0.0, SpatialReference.wgs84()))
59+
}

show-callout/src/main/java/com/esri/arcgismaps/sample/showcallout/screens/MainScreen.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ fun MainScreen(sampleName: String, application: Application) {
4747
ComposeMapView(
4848
modifier = Modifier.fillMaxSize(),
4949
mapViewModel = mapViewModel,
50-
application = application
50+
onSingleTap = { singleTapConfirmedEvent ->
51+
mapViewModel.onMapTapped(singleTapConfirmedEvent.mapPoint)
52+
}
53+
5154
)
5255
}
5356
}

0 commit comments

Comments
 (0)