Skip to content

Commit

Permalink
#267 feat : grid based clustering 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
BENDENG1 committed Feb 8, 2024
1 parent 650b4dd commit bbda470
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,50 @@ class ClusterManager @Inject constructor() {
cameraLongitude: Double,
cameraRadius: Double,
) {
val visibleMarkers = getVisibleMarkers(markers,cameraLatitude,cameraLongitude,cameraRadius)
val newClusters = clusterMarkers(visibleMarkers, cameraRadius)
// val visibleMarkers = getVisibleMarkers(markers,cameraLatitude,cameraLongitude,cameraRadius)
val newClusters = clusterMarkers(markers, cameraRadius)
_clusteredMarkers.value = newClusters
}

fun getClusterList(): List<Cluster> {
return clusteredMarkers.value
}

private fun getVisibleMarkers(
markersList: List<UiRestaurantData>,
cameraLatitude: Double,
cameraLongitude: Double,
cameraRadius: Double
): List<UiRestaurantData> {
val visibleMarkers = mutableListOf<UiRestaurantData>()

for (marker in markersList) {
val distance = haversineDistance(
cameraLatitude,
cameraLongitude,
marker.latitude,
marker.longitude
)

if (distance <= cameraRadius * 2) {
visibleMarkers.add(marker)
}
}

return visibleMarkers
}
// private fun getVisibleMarkers(
// markersList: List<UiRestaurantData>,
// cameraLatitude: Double,
// cameraLongitude: Double,
// cameraRadius: Double
// ): List<UiRestaurantData> {
// val visibleMarkers = mutableListOf<UiRestaurantData>()
//
// for (marker in markersList) {
// val distance = haversineDistance(
// cameraLatitude,
// cameraLongitude,
// marker.latitude,
// marker.longitude
// )
//
// if (distance <= cameraRadius) {
// visibleMarkers.add(marker)
// }
// }
//
// return visibleMarkers
// }

private fun clusterMarkers(
markers: List<UiRestaurantData>,
cameraRadius: Double
): List<Cluster> {
val clusteredMarkers = mutableListOf<Cluster>()
if(cameraRadius < 150)
return emptyList()

val clusterWidth = cameraRadius
val divisions = 20

val divisionWidth = clusterWidth / divisions
val radius = cameraRadius * 2
val divisions = 10
val divisionWidth = radius / divisions

for (marker in markers) {
var isClustered = false
Expand All @@ -74,7 +75,7 @@ class ClusterManager @Inject constructor() {
marker.longitude
)

if (distance < clusterWidth) {
if (distance < divisionWidth) {
val xDistance = abs(cluster.centerLatitude - marker.latitude)
val yDistance = abs(cluster.centerLongitude - marker.longitude)

Expand Down Expand Up @@ -107,8 +108,8 @@ class ClusterManager @Inject constructor() {
clusteredMarkers.add(newCluster)
}
}
return clusteredMarkers.filter { it.markers.size >= 10 }
}

return clusteredMarkers.filter { it.markers.size >= 5 }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ class HomeViewModel @Inject constructor(
}

fun updateCamera(latitude: Double, longitude: Double, zoom: Double) {
val radius = 2.00.pow(14.00 - uiState.value.cameraZoom) * 1000
_uiState.update { state ->
state.copy(
cameraRadius = 2.00.pow(14.00 - uiState.value.cameraZoom) * 1000,
cameraRadius = radius,
cameraLatitude = latitude,
cameraLongitude = longitude,
cameraZoom = zoom
Expand All @@ -129,7 +130,7 @@ class HomeViewModel @Inject constructor(
uiState.value.markerList,
latitude,
longitude,
uiState.value.cameraRadius
radius
)
updateCluster()
}
Expand Down

0 comments on commit bbda470

Please sign in to comment.