diff --git a/app/src/main/java/net/vonforst/evmap/api/goingelectric/GoingElectricApi.kt b/app/src/main/java/net/vonforst/evmap/api/goingelectric/GoingElectricApi.kt index 0cb47ca7..913b7d13 100644 --- a/app/src/main/java/net/vonforst/evmap/api/goingelectric/GoingElectricApi.kt +++ b/app/src/main/java/net/vonforst/evmap/api/goingelectric/GoingElectricApi.kt @@ -19,7 +19,7 @@ interface GoingElectricApi { @Query("ne_lat") ne_lat: Double, @Query("ne_lng") ne_lng: Double, @Query("clustering") clustering: Boolean, @Query("zoom") zoom: Float, - @Query("cluster_distance") clusterDistance: Int, + @Query("cluster_distance") clusterDistance: Int?, @Query("freecharging") freecharging: Boolean, @Query("freeparking") freeparking: Boolean, @Query("min_power") minPower: Int, diff --git a/app/src/main/java/net/vonforst/evmap/viewmodel/MapViewModel.kt b/app/src/main/java/net/vonforst/evmap/viewmodel/MapViewModel.kt index 49cee3ce..f73037be 100644 --- a/app/src/main/java/net/vonforst/evmap/viewmodel/MapViewModel.kt +++ b/app/src/main/java/net/vonforst/evmap/viewmodel/MapViewModel.kt @@ -21,6 +21,16 @@ import retrofit2.Response data class MapPosition(val bounds: LatLngBounds, val zoom: Float) +internal fun getClusterDistance(zoom: Float): Int? { + return when (zoom) { + in 0.0..7.0 -> 100 + in 7.0..11.5 -> 75 + in 11.5..12.5 -> 60 + in 12.5..13.0 -> 45 + else -> null + } +} + class MapViewModel(application: Application, geApiKey: String) : AndroidViewModel(application) { private var api = GoingElectricApi.create(geApiKey, context = application) private var db = AppDatabase.getInstance(application) @@ -166,13 +176,16 @@ class MapViewModel(application: Application, geApiKey: String) : AndroidViewMode } // do not use clustering if filters need to be applied locally. - val useClustering = minConnectors <= 1 + val useClustering = minConnectors <= 1 && zoom < 13 + val clusterDistance = if (useClustering) getClusterDistance(zoom) else null + + println("$zoom, $clusterDistance") val response = api.getChargepoints( bounds.southwest.latitude, bounds.southwest.longitude, bounds.northeast.latitude, bounds.northeast.longitude, - clustering = zoom < 13 && useClustering, zoom = zoom, - clusterDistance = 40, freecharging = freecharging, minPower = minPower, + clustering = useClustering, zoom = zoom, + clusterDistance = clusterDistance, freecharging = freecharging, minPower = minPower, freeparking = freeparking, plugs = connectors ) diff --git a/app/src/test/java/net/vonforst/evmap/viewmodel/MapViewModelTest.kt b/app/src/test/java/net/vonforst/evmap/viewmodel/MapViewModelTest.kt new file mode 100644 index 00000000..60be6c44 --- /dev/null +++ b/app/src/test/java/net/vonforst/evmap/viewmodel/MapViewModelTest.kt @@ -0,0 +1,24 @@ +package net.vonforst.evmap.viewmodel + +import org.junit.Test + +class MapViewModelTest { + @Test + fun testGetClusterDistance() { + var zoom = 0.0f + var previousDistance: Int? = 999 + while (zoom < 20.0f) { + val distance = getClusterDistance(zoom) + if (previousDistance != null) { + if (distance != null) { + assert(distance <= previousDistance) + } + } else { + assert(distance == null) + } + + previousDistance = distance + zoom += 0.1f + } + } +} \ No newline at end of file