11package com.mapconductor.example.pages.animation
22
3- import androidx.compose.runtime.MutableState
4- import androidx.compose.runtime.derivedStateOf
53import androidx.compose.runtime.getValue
6- import androidx.compose.runtime.mutableStateOf
74import androidx.compose.ui.graphics.Color
8- import androidx.compose.ui.unit.dp
95import androidx.lifecycle.ViewModel
10- import com.mapconductor.core.circle.CircleClickEvent
11- import com.mapconductor.core.circle.CircleState
126import com.mapconductor.core.features.GeoPoint
137import com.mapconductor.core.map.MapCameraPosition
148import com.mapconductor.core.map.MapViewState
159import com.mapconductor.core.marker.DefaultIcon
1610import com.mapconductor.core.marker.MarkerAnimation
1711import com.mapconductor.core.marker.MarkerState
18- import com.mapconductor.core.spherical.calculatePositionAtDistance
19- import com.mapconductor.core.spherical.haversineDistance
2012import com.mapconductor.example.toast.ToastMessage
2113import kotlinx.coroutines.flow.MutableStateFlow
2214import kotlinx.coroutines.flow.StateFlow
@@ -28,24 +20,11 @@ interface AnimationPageViewModel {
2820 val messages: StateFlow <List <ToastMessage >>
2921
3022 val circleCenter: GeoPoint
31- val radiusMeters: Double
32- val centerMarker: MarkerState
33- val edgeMarker: MarkerState
34- val circleState: CircleState
23+ val bounceMarker: MarkerState
3524
3625 fun onMapViewChanged (state : MapViewState <* >)
3726
3827 fun onMarkerClick (clicked : MarkerState )
39-
40- fun onMapClick (clicked : GeoPoint )
41-
42- fun onCircleClick (event : CircleClickEvent )
43-
44- fun onMarkerDrag (dragged : MarkerState )
45-
46- fun showToast (text : String )
47-
48- fun removeToast (toastMessage : ToastMessage )
4928}
5029
5130class AnimationPageViewModelImpl :
@@ -62,7 +41,6 @@ class AnimationPageViewModelImpl :
6241 Color .LightGray .copy(alpha = 0.2f ),
6342 Color .Magenta .copy(alpha = 0.2f ),
6443 )
65- private var tapIdx = 0
6644
6745 override val initCameraPosition =
6846 MapCameraPosition (
@@ -79,58 +57,23 @@ class AnimationPageViewModelImpl :
7957
8058 override val circleCenter = GeoPoint .fromLatLong(21.382314 , - 157.933097 )
8159
82- override val centerMarker =
60+ private val _bounceMarker =
8361 MarkerState (
84- id = " center_marker " ,
62+ id = " bounce_marker " ,
8563 position = circleCenter,
8664 icon =
8765 DefaultIcon (
88- fillColor = Color . Red ,
66+ fillColor = colors[ 0 ] ,
8967 strokeColor = Color .White ,
90- label = " C " ,
68+ label = " B " ,
9169 ),
92- draggable = false ,
93- )
94-
95- private val _edgeMarker : MutableState <MarkerState > =
96- mutableStateOf(
97- MarkerState (
98- id = " edge_marker" ,
99- position =
100- calculatePositionAtDistance(
101- center = circleCenter,
102- distanceMeters = 1000.0 ,
103- bearingDegrees = 90.0 , // East
104- ),
105- icon =
106- DefaultIcon (
107- fillColor = Color .Green ,
108- strokeColor = Color .White ,
109- label = " E" ,
110- ),
111- draggable = true ,
112- ),
70+ // If the marker is set animation on creating an instance,
71+ // the marker will be animated when the map will be opened.
72+ animation = MarkerAnimation .Bounce ,
11373 )
114- override val edgeMarker: MarkerState
115- get() = _edgeMarker .value
116-
117- override val radiusMeters by derivedStateOf {
118- haversineDistance(circleCenter, _edgeMarker .value.position)
119- }
12074
121- private val _circleState : MutableState <CircleState > =
122- mutableStateOf(
123- CircleState (
124- id = " circle" ,
125- center = circleCenter,
126- radiusMeters = 1000.0 , // Initial radius
127- strokeColor = Color .Blue .copy(alpha = 0.5f ),
128- strokeWidth = 2 .dp,
129- fillColor = this .colors[0 ],
130- ),
131- )
132- override val circleState: CircleState
133- get() = _circleState .value
75+ override val bounceMarker: MarkerState
76+ get() = _bounceMarker
13477
13578 private val _mapViewState = MutableStateFlow <MapViewState <* >? > (null )
13679 override val mapViewState: StateFlow <MapViewState <* >? > = _mapViewState .asStateFlow()
@@ -140,37 +83,7 @@ class AnimationPageViewModelImpl :
14083 }
14184
14285 override fun onMarkerClick (clicked : MarkerState ) {
86+ // When you want to activate the marker, set the animation for the marker.
14387 clicked.animation = MarkerAnimation .Bounce
14488 }
145-
146- override fun onMapClick (clicked : GeoPoint ) {
147- showToast(" Map clicked at: ${clicked.toUrlValue()} " )
148- }
149-
150- override fun onCircleClick (event : CircleClickEvent ) {
151- this .tapIdx = (this .tapIdx + 1 ) % this .colors.size
152- event.state.fillColor = this .colors[this .tapIdx]
153- showToast(" Circle clicked - Radius: ${radiusMeters.toInt()} m" )
154- }
155-
156- override fun onMarkerDrag (dragged : MarkerState ) {
157- _edgeMarker .value.position = dragged.position
158-
159- // Update circle radius
160- _circleState .value.radiusMeters = radiusMeters // haversineDistance(circleCenter, _edgeMarker.value.position)
161-
162- // showToast("Radius updated: ${radiusMeters.toInt()}m")
163- }
164-
165- override fun showToast (text : String ) {
166- this ._messages .value = this ._messages .value + ToastMessage (text = text)
167- }
168-
169- override fun removeToast (toastMessage : ToastMessage ) {
170- this ._messages .value = this ._messages .value.filter { it != toastMessage }
171- }
172-
173- override fun onCleared () {
174- super .onCleared()
175- }
17689}
0 commit comments