forked from yy0ung/nibobnebob
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
Aos/presentation/src/main/java/com/avengers/presentation/ui/main/home/ClusterManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.avengers.presentation.ui.main.home | ||
|
||
import com.avengers.nibobnebob.presentation.ui.main.home.model.UiRestaurantData | ||
import com.avengers.presentation.util.DistanceUtil.haversineDistance | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import javax.inject.Inject | ||
import kotlin.math.abs | ||
|
||
|
||
class ClusterManager @Inject constructor() { | ||
|
||
private val _clusteredMarkers = MutableStateFlow<List<Cluster>>(emptyList()) | ||
val clusteredMarkers: StateFlow<List<Cluster>> = _clusteredMarkers | ||
|
||
fun updateCluster( | ||
markers: List<UiRestaurantData>, | ||
cameraLatitude: Double, | ||
cameraLongitude: Double, | ||
cameraRadius: Double, | ||
) { | ||
val visibleMarkers = getVisibleMarkers(markers,cameraLatitude,cameraLongitude,cameraRadius) | ||
val newClusters = clusterMarkers(visibleMarkers, 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 clusterMarkers( | ||
markers: List<UiRestaurantData>, | ||
cameraRadius: Double | ||
): List<Cluster> { | ||
val clusteredMarkers = mutableListOf<Cluster>() | ||
|
||
val clusterWidth = cameraRadius | ||
val divisions = 20 | ||
|
||
val divisionWidth = clusterWidth / divisions | ||
|
||
for (marker in markers) { | ||
var isClustered = false | ||
|
||
for (cluster in clusteredMarkers) { | ||
val distance = haversineDistance( | ||
cluster.centerLatitude, | ||
cluster.centerLongitude, | ||
marker.latitude, | ||
marker.longitude | ||
) | ||
|
||
if (distance < clusterWidth) { | ||
val xDistance = abs(cluster.centerLatitude - marker.latitude) | ||
val yDistance = abs(cluster.centerLongitude - marker.longitude) | ||
|
||
val xDivision = (xDistance / divisionWidth).toInt() | ||
val yDivision = (yDistance / divisionWidth).toInt() | ||
|
||
val clusterIndex = xDivision * divisions + yDivision | ||
|
||
cluster.markers.add(marker) | ||
cluster.clusterId.add(clusterIndex) | ||
|
||
isClustered = true | ||
break | ||
} | ||
} | ||
|
||
if (!isClustered) { | ||
val xDivision = ((marker.latitude % divisionWidth) / divisionWidth).toInt() | ||
val yDivision = ((marker.longitude % divisionWidth) / divisionWidth).toInt() | ||
|
||
val clusterIndex = xDivision * divisions + yDivision | ||
|
||
val newCluster = Cluster( | ||
marker.latitude, | ||
marker.longitude, | ||
mutableListOf(marker), | ||
mutableListOf(clusterIndex) | ||
) | ||
|
||
clusteredMarkers.add(newCluster) | ||
} | ||
} | ||
return clusteredMarkers.filter { it.markers.size >= 10 } | ||
} | ||
|
||
|
||
} |