From 18bc70d4fa7453c1191fc446327d1e24d5defe6d Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Sun, 16 Jan 2022 02:31:26 -0600 Subject: Set zoom limits of mapControl by converting zoom levels to Mapnik scale denominators --- .../TrackerMap/android/map/MapFragment.kt | 46 ++++++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-) (limited to 'androidApp/src/main') diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt index 4a63e65..9fa37c9 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt @@ -18,6 +18,7 @@ import mil.nga.sf.wkt.GeometryReader import java.io.File import mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.client.models.Geofence +import mx.trackermap.TrackerMap.utils.MapCalculus import mx.trackermap.TrackerMap.utils.MarkerType typealias MarkerCallback = (Int?) -> Unit @@ -60,7 +61,10 @@ class MapFragment : GlobeMapFragment() { if (tileInfo == null) { val layer = resources.getStringArray(R.array.maps_streets_tile_url) val tmpInfo = RemoteTileInfoNew(layer[0], layer[1].toInt(), layer[2].toInt()) - tileInfo = makeTileInfo(layer[0], tmpInfo) + tileInfo = tileInfoSetCacheDir(layer[0], tmpInfo) + tileInfo?.let { + setZoomLimits(it.minZoom, it.maxZoom) + } } val params = SamplingParams() @@ -77,7 +81,16 @@ class MapFragment : GlobeMapFragment() { val latitude = 23.191 val longitude = -100.36 - focusOn(latitude, longitude, zoom = 0.4, animated = false) + focusOn(latitude, longitude, height = 0.4, animated = false) + } + + override fun mapDidStopMoving( + mapControl: MapController?, + corners: Array?, + userMotion: Boolean + ) { + super.mapDidStopMoving(mapControl, corners, userMotion) + Log.d("MapFragment", "Height: ${mapControl?.height}") } override fun userDidSelect( @@ -221,7 +234,8 @@ class MapFragment : GlobeMapFragment() { mbr.expandByFraction(0.1) mapControl.addPostSurfaceRunnable { - val zoom = mapControl.findHeightToViewBounds(mbr, mbr.middle()) + val zoom = mapControl.zoomLimitMax.coerceAtLeast( + mapControl.findHeightToViewBounds(mbr, mbr.middle())) mapControl.setPositionGeo(mbr.middle(), zoom) } } @@ -301,17 +315,19 @@ class MapFragment : GlobeMapFragment() { )) } - fun focusOn(latitude: Double, longitude: Double, zoom: Double = 0.00009, animated: Boolean = true) { + fun focusOn(latitude: Double, longitude: Double, height: Double = 0.00009, animated: Boolean = true) { val lat = latitude * Math.PI / 180 val lon = longitude * Math.PI / 180 + // Ensure height is equal or higher than bottom limit + val z = mapControl.zoomLimitMin.coerceAtLeast(height) if (animated) { - mapControl.animatePositionGeo(lon, lat, zoom, 0.2) + mapControl.animatePositionGeo(lon, lat, z, 0.2) } else { - mapControl.setPositionGeo(lon, lat, zoom) + mapControl.setPositionGeo(lon, lat, z) } } - private fun makeTileInfo(url: String, tileInfo: TileInfoNew): TileInfoNew? { + private fun tileInfoSetCacheDir(url: String, tileInfo: TileInfoNew): TileInfoNew? { return context?.let { val cacheDirName = url.toByteArray(Charsets.UTF_8).md5().hex val cacheDirMap = File(it.cacheDir, cacheDirName) @@ -323,11 +339,25 @@ class MapFragment : GlobeMapFragment() { } } + private fun setZoomLimits(minZoom: Int, maxZoom: Int) { + mapControl?.setZoomLimits( + mapControl.heightForMapScale( + MapCalculus.zoomLevelToScale(maxZoom) + ?: MapCalculus.zoomLevelToScale(20)!! + ), + mapControl.heightForMapScale( + MapCalculus.zoomLevelToScale(minZoom) + ?: MapCalculus.zoomLevelToScale(1)!! + ) + ) + } + fun updateTileInfo(url: String, tileInfo: TileInfoNew) { context?.let { - this.tileInfo = makeTileInfo(url, tileInfo) + this.tileInfo = tileInfoSetCacheDir(url, tileInfo) this.tileInfo?.let { loader?.changeTileInfo(it) + setZoomLimits(tileInfo.minZoom, tileInfo.maxZoom) } } } -- cgit v1.2.3