@@ -24,6 +24,7 @@ import androidx.core.content.PermissionChecker
24
24
import androidx.lifecycle.Lifecycle
25
25
import androidx.lifecycle.LifecycleEventObserver
26
26
import androidx.lifecycle.compose.LocalLifecycleOwner
27
+ import androidx.lifecycle.compose.collectAsStateWithLifecycle
27
28
import com.google.android.gms.location.FusedLocationProviderClient
28
29
import com.google.android.gms.location.LocationServices
29
30
import com.naver.maps.geometry.LatLng
@@ -68,13 +69,24 @@ fun NaverMap(
68
69
val lifecycleOwner = LocalLifecycleOwner .current
69
70
val coroutineScope = rememberCoroutineScope()
70
71
72
+ val centerLatLng by mapViewModel.centerLatLng.collectAsStateWithLifecycle()
73
+
71
74
LaunchedEffect (lastLocation) {
72
75
// 현재 위치와 마지막 위치가 5미터 이상 차이가 날때만 현위치 기준 반경 100m 픽 정보 개수 불러오기
73
76
lastLocation?.let {
74
77
mapViewModel.requestPickNotificationArea(lastLocation, CIRCLE_RADIUS_METER )
75
78
}
76
79
}
77
80
81
+ LaunchedEffect (centerLatLng) {
82
+ naverMap.value?.projection?.fromScreenLocation(PointF (0F , 0F ))?.run {
83
+ mapViewModel.fetchPicksInBounds(
84
+ leftTop = this ,
85
+ clusterer = clusterer
86
+ )
87
+ }
88
+ }
89
+
78
90
DisposableEffect (Unit ) {
79
91
clusterer = buildClusterer(context, mapViewModel)
80
92
@@ -121,16 +133,21 @@ fun NaverMap(
121
133
mapType = NaverMap .MapType .Navi
122
134
initMapSettings()
123
135
initDeviceLocation(
124
- context,
125
- circleOverlay,
126
- fusedLocationClient,
127
- mapViewModel.lastCameraPosition
128
- )
136
+ context = context,
137
+ circleOverlay = circleOverlay,
138
+ fusedLocationClient = fusedLocationClient,
139
+ lastCameraPosition = mapViewModel.lastCameraPosition
140
+ ) {
141
+ mapViewModel.fetchPicksInBounds(
142
+ leftTop = this .projection.fromScreenLocation(PointF (0F , 0F )),
143
+ clusterer = clusterer
144
+ )
145
+ }
129
146
initLocationOverlay(locationSource, locationOverlay)
130
147
setLocationChangeListener(circleOverlay, mapViewModel)
131
148
setMapClickListener { mapViewModel.resetClickedMarkerState(context) }
132
- setCameraIdleListener { leftTop, rightBottom ->
133
- mapViewModel.fetchPicksInBounds(leftTop, rightBottom, clusterer )
149
+ setCameraIdleListener { centerLatLng ->
150
+ mapViewModel.updateCenterLatLng(centerLatLng )
134
151
}
135
152
clusterer?.map = this
136
153
}
@@ -172,7 +189,8 @@ private fun NaverMap.initDeviceLocation(
172
189
context : Context ,
173
190
circleOverlay : CircleOverlay ,
174
191
fusedLocationClient : FusedLocationProviderClient ,
175
- lastCameraPosition : CameraPosition ?
192
+ lastCameraPosition : CameraPosition ? ,
193
+ fetchPicksInBounds : () -> Unit ,
176
194
) {
177
195
if (checkSelfPermission(context)) {
178
196
fusedLocationClient.lastLocation.addOnSuccessListener { location ->
@@ -181,6 +199,7 @@ private fun NaverMap.initDeviceLocation(
181
199
setCircleOverlay(circleOverlay, location)
182
200
lastCameraPosition?.let {
183
201
moveCamera(CameraUpdate .toCameraPosition(it))
202
+ fetchPicksInBounds()
184
203
} ? : run {
185
204
moveCamera(CameraUpdate .scrollTo(LatLng (location)))
186
205
moveCamera(CameraUpdate .zoomTo(INITIAL_CAMERA_ZOOM ))
@@ -237,13 +256,10 @@ private fun NaverMap.setMapClickListener(
237
256
238
257
// 카메라 대기 이벤트 설정
239
258
private fun NaverMap.setCameraIdleListener (
240
- fetchPicksInBounds : (LatLng , LatLng ) -> Unit
259
+ updateCenterLatLng : (LatLng ) -> Unit
241
260
) {
242
261
addOnCameraIdleListener {
243
- val leftTop = projection.fromScreenLocation(PointF (0f , 0f ))
244
- val rightBottom =
245
- projection.fromScreenLocation(PointF (contentWidth.toFloat(), contentHeight.toFloat()))
246
- fetchPicksInBounds(leftTop, rightBottom)
262
+ updateCenterLatLng(cameraPosition.target)
247
263
}
248
264
}
249
265
0 commit comments