@@ -2,11 +2,11 @@ package com.whyranoid.presentation.running
2
2
3
3
import android.os.Bundle
4
4
import androidx.activity.viewModels
5
- import com.naver.maps.geometry.LatLng
6
- import com.naver.maps.map.CameraUpdate
7
5
import com.naver.maps.map.MapView
8
6
import com.naver.maps.map.NaverMap
9
7
import com.naver.maps.map.OnMapReadyCallback
8
+ import com.naver.maps.map.overlay.OverlayImage
9
+ import com.naver.maps.map.overlay.PathOverlay
10
10
import com.whyranoid.presentation.R
11
11
import com.whyranoid.presentation.base.BaseActivity
12
12
import com.whyranoid.presentation.databinding.ActivityRunningBinding
@@ -23,38 +23,38 @@ internal class RunningActivity :
23
23
24
24
private lateinit var mapView: MapView
25
25
private lateinit var naverMap: NaverMap
26
+ private lateinit var paths: MutableList <PathOverlay >
26
27
27
28
override fun onCreate (savedInstanceState : Bundle ? ) {
28
29
super .onCreate(savedInstanceState)
29
- mapView = binding.mapView
30
-
31
- mapView.onCreate(savedInstanceState)
32
- mapView.getMapAsync(this )
33
30
34
- binding.vm = viewModel
35
-
36
- repeatWhenUiStarted {
37
- viewModel.runningState.collect { runningState ->
38
- with (runningState.runningData) {
39
- binding.tvStartTime.text = Date (startTime).dateToString(" hh:mm" )
40
- binding.tvRunningTime.text =
41
- String .format(" %d:%02d" , runningTime / 60 , runningTime % 60 )
42
- binding.tvTotalDistance.text = String .format(" %.4f m" , totalDistance)
43
- binding.tvPace.text = String .format(" %.4f km/h" , pace * 3.6 )
44
- }
45
- }
46
- }
31
+ initViews(savedInstanceState)
32
+ observeState()
47
33
}
48
34
49
35
override fun onMapReady (naverMap : NaverMap ) {
50
36
this .naverMap = naverMap
51
37
52
- naverMap.maxZoom = 18.0
53
- naverMap.minZoom = 10.0
38
+ with (naverMap) {
39
+ maxZoom = MAP_MAX_ZOOM
40
+ minZoom = MAP_MIN_ZOOM
41
+ uiSettings.isLocationButtonEnabled = false
42
+ uiSettings.isZoomControlEnabled = false
43
+ locationOverlay.isVisible = true
44
+ locationOverlay.icon = OverlayImage .fromResource(R .drawable.kong)
45
+ locationOverlay.iconWidth = MAP_ICON_SIZE
46
+ locationOverlay.iconHeight = MAP_ICON_SIZE
47
+ }
48
+
49
+ paths = mutableListOf ()
54
50
55
- naverMap.moveCamera(CameraUpdate .scrollAndZoomTo(LatLng (37.498095 , 127.027610 ), 15.0 ))
56
- naverMap.uiSettings.isLocationButtonEnabled = false
57
- naverMap.uiSettings.isZoomControlEnabled = false
51
+ repeatWhenUiStarted {
52
+ viewModel.runningState.collect { runningState ->
53
+ makeNewPath(runningState)
54
+ updateRunnerPosition(runningState)
55
+ updatePathsOverlay(runningState)
56
+ }
57
+ }
58
58
}
59
59
60
60
override fun onStart () {
@@ -91,4 +91,60 @@ internal class RunningActivity :
91
91
super .onLowMemory()
92
92
mapView.onLowMemory()
93
93
}
94
+
95
+ private fun initViews (savedInstanceState : Bundle ? ) {
96
+ mapView = binding.mapView
97
+
98
+ mapView.onCreate(savedInstanceState)
99
+ mapView.getMapAsync(this )
100
+
101
+ binding.vm = viewModel
102
+ }
103
+
104
+ private fun observeState () {
105
+ repeatWhenUiStarted {
106
+ viewModel.runningState.collect { runningState ->
107
+ with (runningState.runningData) {
108
+ binding.tvStartTime.text = Date (startTime).dateToString(" hh:mm" )
109
+ binding.tvRunningTime.text =
110
+ String .format(" %d:%02d" , runningTime / 60 , runningTime % 60 )
111
+ binding.tvTotalDistance.text = String .format(" %.4f m" , totalDistance)
112
+ binding.tvPace.text = String .format(" %.4f km/h" , pace * 3.6 )
113
+ }
114
+ }
115
+ }
116
+ }
117
+
118
+ private fun provideMogakrunPath (): PathOverlay {
119
+ return PathOverlay ().apply {
120
+ color = getColor(R .color.mogakrun_secondary_light)
121
+ width = 20
122
+ }
123
+ }
124
+
125
+ private fun makeNewPath (runningState : RunningState ) {
126
+ while (paths.size < runningState.runningData.runningPositionList.size) {
127
+ paths.add(provideMogakrunPath())
128
+ }
129
+ }
130
+
131
+ private fun updateRunnerPosition (runningState : RunningState ) {
132
+ runningState.runningData.runningPositionList.last().lastOrNull()?.let {
133
+ naverMap.locationOverlay.position = it.toLatLng()
134
+ }
135
+ }
136
+
137
+ private fun updatePathsOverlay (runningState : RunningState ) {
138
+ if (runningState.runningData.runningPositionList.last().size >= 2 ) {
139
+ paths.last().coords =
140
+ runningState.runningData.runningPositionList.last().map { it.toLatLng() }
141
+ paths.last().map = naverMap
142
+ }
143
+ }
144
+
145
+ companion object {
146
+ const val MAP_MAX_ZOOM = 18.0
147
+ const val MAP_MIN_ZOOM = 10.0
148
+ const val MAP_ICON_SIZE = 80
149
+ }
94
150
}
0 commit comments