From 09307f3a39a7130907fb3d84d4fbcdf2363ef2da Mon Sep 17 00:00:00 2001 From: "a.kalmakhanov" Date: Sun, 17 Jul 2022 18:46:57 +0600 Subject: [PATCH] Added new feature to draw routes with multiple points --- demo/src/jvmMain/kotlin/Main.kt | 13 ++++++++++++- .../sciprog/maps/compose/FeatureBuilder.kt | 10 ++++++++++ .../centre/sciprog/maps/compose/MapFeature.kt | 12 ++++++++++++ .../centre/sciprog/maps/compose/MapViewJvm.kt | 16 +++++++++++----- 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/demo/src/jvmMain/kotlin/Main.kt b/demo/src/jvmMain/kotlin/Main.kt index 18594d4..d26234e 100644 --- a/demo/src/jvmMain/kotlin/Main.kt +++ b/demo/src/jvmMain/kotlin/Main.kt @@ -54,7 +54,7 @@ fun App() { ) { val pointOne = 55.568548 to 37.568604 val pointTwo = 55.929444 to 37.518434 - val pointThree = 60.929444 to 37.518434 + val pointThree = 55.742465 to 37.615812 image(pointOne, Icons.Filled.Home) @@ -63,6 +63,17 @@ fun App() { centerCoordinates = pointTwo, ) + route( + route = listOf( + 55.742465 to 37.615812, + 55.742713 to 37.616370, + 55.742815 to 37.616659, + 55.742320 to 37.617132, + 55.742086 to 37.616566, + 55.741715 to 37.616716 + ) + ) + custom(position = pointThree) { drawRect( color = Color.Red, diff --git a/maps-kt-compose/src/commonMain/kotlin/centre/sciprog/maps/compose/FeatureBuilder.kt b/maps-kt-compose/src/commonMain/kotlin/centre/sciprog/maps/compose/FeatureBuilder.kt index 9a7e38f..493c292 100644 --- a/maps-kt-compose/src/commonMain/kotlin/centre/sciprog/maps/compose/FeatureBuilder.kt +++ b/maps-kt-compose/src/commonMain/kotlin/centre/sciprog/maps/compose/FeatureBuilder.kt @@ -8,6 +8,7 @@ import androidx.compose.ui.geometry.Size import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.vector.ImageVector +import centre.sciprog.maps.GeodeticMapCoordinates import centre.sciprog.maps.GmcBox typealias FeatureId = String @@ -23,6 +24,7 @@ internal class MapFeatureBuilder(initialFeatures: Map) : private val content: SnapshotStateMap = mutableStateMapOf().apply { putAll(initialFeatures) } + private fun generateID(feature: MapFeature): FeatureId = "@feature[${feature.hashCode().toUInt()}]" override fun addFeature(id: FeatureId?, feature: MapFeature): FeatureId { @@ -34,6 +36,14 @@ internal class MapFeatureBuilder(initialFeatures: Map) : override fun build(): SnapshotStateMap = content } +fun FeatureBuilder.route( + route: List>, + zoomRange: IntRange = defaultZoomRange, + stroke: Float = 2f, + color: Color = Color.Red, + id: FeatureId? = null +) = addFeature(id, MapRouteFeature(route.map { it.toCoordinates() }, zoomRange, stroke, color)) + fun FeatureBuilder.circle( centerCoordinates: Pair, zoomRange: IntRange = defaultZoomRange, diff --git a/maps-kt-compose/src/commonMain/kotlin/centre/sciprog/maps/compose/MapFeature.kt b/maps-kt-compose/src/commonMain/kotlin/centre/sciprog/maps/compose/MapFeature.kt index 8b7defb..6761371 100644 --- a/maps-kt-compose/src/commonMain/kotlin/centre/sciprog/maps/compose/MapFeature.kt +++ b/maps-kt-compose/src/commonMain/kotlin/centre/sciprog/maps/compose/MapFeature.kt @@ -40,6 +40,18 @@ abstract class MapCustomFeature( abstract fun drawFeature(drawScope: DrawScope, offset: Offset) } +class MapRouteFeature( + val route: List, + zoomRange: IntRange = defaultZoomRange, + val stroke: Float = 2f, + val color: Color = Color.Red +) : MapFeature(zoomRange) { + override fun getBoundingBox(zoom: Int): GmcBox { + return GmcBox(route.first(), route.last()) + } + +} + class MapCircleFeature( val center: GeodeticMapCoordinates, zoomRange: IntRange = defaultZoomRange, diff --git a/maps-kt-compose/src/jvmMain/kotlin/centre/sciprog/maps/compose/MapViewJvm.kt b/maps-kt-compose/src/jvmMain/kotlin/centre/sciprog/maps/compose/MapViewJvm.kt index 6fe7697..81247ba 100644 --- a/maps-kt-compose/src/jvmMain/kotlin/centre/sciprog/maps/compose/MapViewJvm.kt +++ b/maps-kt-compose/src/jvmMain/kotlin/centre/sciprog/maps/compose/MapViewJvm.kt @@ -9,11 +9,8 @@ import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Rect -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.graphics.PathEffect +import androidx.compose.ui.graphics.* import androidx.compose.ui.graphics.drawscope.* -import androidx.compose.ui.graphics.nativeCanvas -import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.input.pointer.* import androidx.compose.ui.unit.* import centre.sciprog.maps.* @@ -236,10 +233,19 @@ actual fun MapView( feature.color.toPaint() ) } - is MapCustomFeature -> drawIntoCanvas { canvas -> + is MapCustomFeature -> { val offset = feature.position.toOffset() feature.drawFeature(this, offset) } + is MapRouteFeature -> { + val points = feature.route.map { it.toOffset() } + drawPoints( + points = points, + color = feature.color, + pointMode = PointMode.Polygon, + strokeWidth = feature.stroke + ) + } } }