From 4dc8f7cdfcdbf5d6de57338be80477d8433504f1 Mon Sep 17 00:00:00 2001 From: "a.kalmakhanov" Date: Fri, 22 Jul 2022 11:48:44 +0600 Subject: [PATCH] OnFeatureClick removed --- demo/src/jvmMain/kotlin/Main.kt | 20 ++++++++++++++----- .../centre/sciprog/maps/compose/MapView.kt | 20 ++++++++++--------- .../centre/sciprog/maps/compose/MapViewJvm.kt | 10 +++++++++- .../kotlin/centre/sciprog/maps/Utils.kt | 7 +++++++ 4 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 maps-kt-core/src/commonMain/kotlin/centre/sciprog/maps/Utils.kt diff --git a/demo/src/jvmMain/kotlin/Main.kt b/demo/src/jvmMain/kotlin/Main.kt index 3363848..d6cf681 100644 --- a/demo/src/jvmMain/kotlin/Main.kt +++ b/demo/src/jvmMain/kotlin/Main.kt @@ -12,6 +12,7 @@ import androidx.compose.ui.window.application import centre.sciprog.maps.GeodeticMapCoordinates import centre.sciprog.maps.MapViewPoint import centre.sciprog.maps.compose.* +import centre.sciprog.maps.toDegrees import io.ktor.client.HttpClient import io.ktor.client.engine.cio.CIO import kotlinx.coroutines.delay @@ -47,18 +48,27 @@ 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 + } 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) @@ -75,7 +85,7 @@ fun App() { ) } - line(pointOne, pointTwo) + line(pointOne, pointTwo, id = "Line") text(pointOne, "Home") centerCoordinates?.let { diff --git a/maps-kt-compose/src/commonMain/kotlin/centre/sciprog/maps/compose/MapView.kt b/maps-kt-compose/src/commonMain/kotlin/centre/sciprog/maps/compose/MapView.kt index f463fd7..54fd614 100644 --- a/maps-kt-compose/src/commonMain/kotlin/centre/sciprog/maps/compose/MapView.kt +++ b/maps-kt-compose/src/commonMain/kotlin/centre/sciprog/maps/compose/MapView.kt @@ -17,6 +17,7 @@ data class MapViewConfig( val onClick: MapViewPoint.() -> Unit = {}, val onViewChange: MapViewPoint.() -> Unit = {}, val onSelect: (GmcBox) -> Unit = {}, + val onDrag: (MapViewPoint, MapViewPoint) -> Boolean = { _, _ -> true }, val zoomOnSelect: Boolean = true ) @@ -49,15 +50,16 @@ fun MapView( ) } -internal fun GmcBox.getComputeViewPoint(mapTileProvider: MapTileProvider): (canvasSize: DpSize) -> MapViewPoint = { canvasSize -> - val zoom = log2( - min( - canvasSize.width.value / width, - canvasSize.height.value / height - ) * PI / mapTileProvider.tileSize - ) - MapViewPoint(center, zoom) -} +internal fun GmcBox.getComputeViewPoint(mapTileProvider: MapTileProvider): (canvasSize: DpSize) -> MapViewPoint = + { canvasSize -> + val zoom = log2( + min( + canvasSize.width.value / width, + canvasSize.height.value / height + ) * PI / mapTileProvider.tileSize + ) + MapViewPoint(center, zoom) + } @Composable fun MapView( 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 7c5b56e..4a577ca 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 @@ -131,7 +131,7 @@ actual fun MapView( rect.bottomRight.toDpOffset().toGeodetic() ) config.onSelect(gmcBox) - if(config.zoomOnSelect) { + if (config.zoomOnSelect) { val newViewPoint = gmcBox.getComputeViewPoint(mapTileProvider).invoke(canvasSize) config.onViewChange(newViewPoint) @@ -144,6 +144,14 @@ actual fun MapView( val dpPos = DpOffset(dragStart.x.toDp(), dragStart.y.toDp()) config.onClick(MapViewPoint(dpPos.toGeodetic(), viewPoint.zoom)) drag(change.id) { dragChange -> + val dpStartPos = + DpOffset(dragChange.previousPosition.x.toDp(), dragChange.previousPosition.y.toDp()) + val dpEndPos = DpOffset(dragChange.position.x.toDp(), dragChange.position.y.toDp()) + if (!config.onDrag( + MapViewPoint(dpStartPos.toGeodetic(), viewPoint.zoom), + MapViewPoint(dpEndPos.toGeodetic(), viewPoint.zoom) + ) + ) return@drag val dragAmount = dragChange.position - dragChange.previousPosition val newViewPoint = viewPoint.move( -dragAmount.x.toDp().value / tileScale, diff --git a/maps-kt-core/src/commonMain/kotlin/centre/sciprog/maps/Utils.kt b/maps-kt-core/src/commonMain/kotlin/centre/sciprog/maps/Utils.kt new file mode 100644 index 0000000..a5c28db --- /dev/null +++ b/maps-kt-core/src/commonMain/kotlin/centre/sciprog/maps/Utils.kt @@ -0,0 +1,7 @@ +package centre.sciprog.maps + +import kotlin.math.PI + +fun Double.toDegrees() = this * 180 / PI + +fun Double.toRadians() = this * PI / 180 \ No newline at end of file -- 2.34.1