fix: calculate download BoundingBox based on zoom factor

instead of calling `setZoom()` before/after `MapView.boundingBox` which can get out of sync and cause inconsistencies in the tile count.
fixes 
pull/674/head
andrekir 2023-07-27 05:56:59 -03:00
rodzic 094af7c380
commit 49617d2e19
2 zmienionych plików z 34 dodań i 5 usunięć
app/src/main/java/com/geeksville/mesh

Wyświetl plik

@ -52,6 +52,7 @@ import com.geeksville.mesh.util.EnableWakeLock
import com.geeksville.mesh.util.SqlTileWriterExt
import com.geeksville.mesh.util.requiredZoomLevel
import com.geeksville.mesh.util.formatAgo
import com.geeksville.mesh.util.zoomIn
import com.geeksville.mesh.waypoint
import com.google.accompanist.themeadapter.appcompat.AppCompatTheme
import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -461,21 +462,21 @@ fun MapView(model: UIViewModel = viewModel()) {
*/
fun generateBoxOverlay(zoomLevel: Double) = map.apply {
overlayManager = CustomOverlayManager(TilesOverlay(tileProvider, context))
val zoomFactor = 1.3 // zoom difference between view and download area polygon
controller.setZoom(zoomLevel - zoomFactor)
setMultiTouchControls(false)
// furthest back
zoomLevelMax = zoomLevelHighest // FIXME zoomLevel
// furthest in min should be > than max
zoomLevelMin = map.tileProvider.tileSource.maximumZoomLevel.toDouble()
controller.setZoom(zoomLevel)
downloadRegionBoundingBox = map.boundingBox
zoomLevelMin = tileProvider.tileSource.maximumZoomLevel.toDouble()
downloadRegionBoundingBox = boundingBox.zoomIn(zoomFactor)
val polygon = Polygon().apply {
points = Polygon.pointsAsRect(downloadRegionBoundingBox).map {
GeoPoint(it.latitude, it.longitude)
}
}
overlayManager.add(polygon)
controller.setZoom(zoomLevel - 1.0)
val tileCount: Int = CacheManager(map).possibleTilesInArea(
val tileCount: Int = CacheManager(this).possibleTilesInArea(
downloadRegionBoundingBox,
zoomLevelMax.toInt(),
zoomLevelMin.toInt()

Wyświetl plik

@ -12,6 +12,7 @@ import kotlin.math.acos
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.log2
import kotlin.math.pow
import kotlin.math.sin
/*******************************************************************************
@ -251,3 +252,30 @@ fun BoundingBox.requiredZoomLevel(): Double {
return maxOf(requiredLatZoom, requiredLonZoom)
}
/**
* Creates a new bounding box with adjusted dimensions based on the provided [zoomFactor].
* @return A new [BoundingBox] with added [zoomFactor]. Example:
* ```
* // Setting the zoom level directly using setZoom()
* map.setZoom(14.0)
* val boundingBoxZoom14 = map.boundingBox
*
* // Using zoomIn() results the equivalent BoundingBox with setZoom(15.0)
* val boundingBoxZoom15 = boundingBoxZoom14.zoomIn(1.0)
* ```
*/
fun BoundingBox.zoomIn(zoomFactor: Double): BoundingBox {
val center = GeoPoint((latNorth + latSouth) / 2, (lonWest + lonEast) / 2)
val latDiff = latNorth - latSouth
val lonDiff = lonEast - lonWest
val newLatDiff = latDiff / (2.0.pow(zoomFactor))
val newLonDiff = lonDiff / (2.0.pow(zoomFactor))
return BoundingBox(
center.latitude + newLatDiff / 2,
center.longitude + newLonDiff / 2,
center.latitude - newLatDiff / 2,
center.longitude - newLonDiff / 2
)
}