diff --git a/demo/maps/src/jvmMain/kotlin/AngleConversion.kt b/demo/maps/src/jvmMain/kotlin/AngleConversion.kt new file mode 100644 index 0000000..a7ec5e5 --- /dev/null +++ b/demo/maps/src/jvmMain/kotlin/AngleConversion.kt @@ -0,0 +1,4 @@ +import kotlin.math.PI + +fun Double.toDegrees() = this * 180 / PI + diff --git a/demo/maps/src/jvmMain/kotlin/Main.kt b/demo/maps/src/jvmMain/kotlin/Main.kt index 45c56e7..8247dc9 100644 --- a/demo/maps/src/jvmMain/kotlin/Main.kt +++ b/demo/maps/src/jvmMain/kotlin/Main.kt @@ -6,7 +6,6 @@ import androidx.compose.material.icons.filled.Home import androidx.compose.runtime.* import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color -import androidx.compose.ui.graphics.PointMode import androidx.compose.ui.window.Window import androidx.compose.ui.window.application import center.sciprog.maps.compose.* @@ -49,32 +48,29 @@ fun App() { var centerCoordinates by remember { mutableStateOf(null) } + val pointOne = 55.568548 to 37.568604 + var pointTwo by remember { mutableStateOf(55.929444 to 37.518434) } + val pointThree = 60.929444 to 37.518434 MapView( mapTileProvider = mapTileProvider, initialViewPoint = viewPoint, config = MapViewConfig( inferViewBoxFromFeatures = true, - onViewChange = { centerCoordinates = focus } + onViewChange = { centerCoordinates = focus }, + onDrag = { start, end -> + if (start.focus.latitude.toDegrees() in (pointTwo.first - 0.05)..(pointTwo.first + 0.05) && + start.focus.longitude.toDegrees() in (pointTwo.second - 0.05)..(pointTwo.second + 0.05) + ) { + pointTwo = pointTwo.first + (end.focus.latitude - start.focus.latitude).toDegrees() to + pointTwo.second + (end.focus.longitude - start.focus.longitude).toDegrees() + false// returning false, because when we are dragging circle we don't want to drag map + } else true + } ) ) { - val pointOne = 55.568548 to 37.568604 - val pointTwo = 55.929444 to 37.518434 - val pointThree = 60.929444 to 37.518434 image(pointOne, Icons.Filled.Home) - points( - points = 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 - ), - pointMode = PointMode.Polygon - ) - //remember feature Id val circleId: FeatureId = circle( centerCoordinates = pointTwo, @@ -87,7 +83,7 @@ fun App() { arc(pointOne, Distance(10.0), 0f, PI) - line(pointOne, pointTwo) + line(pointOne, pointTwo, id = "line") text(pointOne, "Home", font = { size = 32f }) centerCoordinates?.let { diff --git a/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapFeature.kt b/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapFeature.kt index 202e30b..2180674 100644 --- a/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapFeature.kt +++ b/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapFeature.kt @@ -3,7 +3,6 @@ package center.sciprog.maps.compose import androidx.compose.runtime.Composable import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ImageBitmap -import androidx.compose.ui.graphics.PointMode import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.graphics.vector.ImageVector @@ -49,18 +48,6 @@ public class MapDrawFeature( } } -public class MapPointsFeature( - public val points: List, - override val zoomRange: IntRange = defaultZoomRange, - public val stroke: Float = 2f, - public val color: Color = Color.Red, - public val pointMode: PointMode = PointMode.Points -) : MapFeature { - override fun getBoundingBox(zoom: Int): GmcBox { - return GmcBox(points.first(), points.last()) - } -} - public class MapCircleFeature( public val center: GeodeticMapCoordinates, override val zoomRange: IntRange = defaultZoomRange, diff --git a/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapFeatureBuilder.kt b/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapFeatureBuilder.kt index e916f12..55f18ee 100644 --- a/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapFeatureBuilder.kt +++ b/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapFeatureBuilder.kt @@ -4,7 +4,6 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateMapOf import androidx.compose.runtime.snapshots.SnapshotStateMap import androidx.compose.ui.graphics.Color -import androidx.compose.ui.graphics.PointMode import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.unit.DpSize @@ -117,15 +116,6 @@ public fun MapFeatureBuilder.arc( ) ) -public fun MapFeatureBuilder.points( - points: List>, - zoomRange: IntRange = defaultZoomRange, - stroke: Float = 2f, - color: Color = Color.Red, - pointMode: PointMode = PointMode.Points, - id: FeatureId? = null -): FeatureId = addFeature(id, MapPointsFeature(points.map { it.toCoordinates() }, zoomRange, stroke, color, pointMode)) - @Composable public fun MapFeatureBuilder.image( position: Pair, diff --git a/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapView.kt b/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapView.kt index 80e5287..0dc4fd4 100644 --- a/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapView.kt +++ b/maps-kt-compose/src/commonMain/kotlin/center/sciprog/maps/compose/MapView.kt @@ -11,10 +11,15 @@ import kotlin.math.min //TODO consider replacing by modifier +/** + * @param onDrag - returns true if you want to drag a map and false, if you want to make map stationary. + * start - is a point where drag begins, end is a point where drag ends + */ public data class MapViewConfig( val zoomSpeed: Double = 1.0 / 3.0, val inferViewBoxFromFeatures: Boolean = false, val onClick: MapViewPoint.() -> Unit = {}, + val onDrag: (start: MapViewPoint, end: MapViewPoint) -> Boolean = { _, _ -> true }, val onViewChange: MapViewPoint.() -> Unit = {}, val onSelect: (GmcBox) -> Unit = {}, val zoomOnSelect: Boolean = true, diff --git a/maps-kt-compose/src/jvmMain/kotlin/center/sciprog/maps/compose/MapViewJvm.kt b/maps-kt-compose/src/jvmMain/kotlin/center/sciprog/maps/compose/MapViewJvm.kt index e296900..3bd55ad 100644 --- a/maps-kt-compose/src/jvmMain/kotlin/center/sciprog/maps/compose/MapViewJvm.kt +++ b/maps-kt-compose/src/jvmMain/kotlin/center/sciprog/maps/compose/MapViewJvm.kt @@ -146,6 +146,14 @@ public actual fun MapView( config.onClick(MapViewPoint(dpPos.toGeodetic(), viewPoint.zoom)) drag(change.id) { dragChange -> val dragAmount = dragChange.position - dragChange.previousPosition + val dpStart = + DpOffset(dragChange.previousPosition.x.toDp(), dragChange.previousPosition.y.toDp()) + val dpEnd = DpOffset(dragChange.position.x.toDp(), dragChange.position.y.toDp()) + if (!config.onDrag( + MapViewPoint(dpStart.toGeodetic(), viewPoint.zoom), + MapViewPoint(dpEnd.toGeodetic(), viewPoint.zoom) + ) + ) return@drag val newViewPoint = viewPoint.move( -dragAmount.x.toDp().value / tileScale, +dragAmount.y.toDp().value / tileScale @@ -275,15 +283,6 @@ public actual fun MapView( drawFeature(zoom, it) } } - is MapPointsFeature -> { - val points = feature.points.map { it.toOffset() } - drawPoints( - points = points, - color = feature.color, - strokeWidth = feature.stroke, - pointMode = feature.pointMode - ) - } else -> { logger.error { "Unrecognized feature type: ${feature::class}" } }