Add rectangle override for map component builder

This commit is contained in:
Alexander Nozik 2022-09-13 13:44:38 +03:00
parent 1808a9b7d3
commit 4cabfcf16f
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
4 changed files with 23 additions and 19 deletions

View File

@ -10,7 +10,7 @@ val ktorVersion by extra("2.0.3")
allprojects { allprojects {
group = "center.sciprog" group = "center.sciprog"
version = "0.1.0-dev-4" version = "0.1.0-dev-5"
} }
ksciencePublish{ ksciencePublish{

View File

@ -35,12 +35,12 @@ private fun GeodeticMapCoordinates.toShortString(): String =
fun App() { fun App() {
MaterialTheme { MaterialTheme {
//create a view point //create a view point
val viewPoint = remember { // val viewPoint = remember {
MapViewPoint( // MapViewPoint(
GeodeticMapCoordinates.ofDegrees(55.7558, 37.6173), // GeodeticMapCoordinates.ofDegrees(55.7558, 37.6173),
8.0 // 8.0
) // )
} // }
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
val mapTileProvider = remember { val mapTileProvider = remember {

View File

@ -101,20 +101,29 @@ private fun prepareConfig(initialConfig: MapViewConfig, featureBuilder: MapFeatu
internal fun GmcRectangle.computeViewPoint( internal fun GmcRectangle.computeViewPoint(
mapTileProvider: MapTileProvider, mapTileProvider: MapTileProvider,
): (canvasSize: DpSize) -> MapViewPoint = { canvasSize -> canvasSize: DpSize,
): MapViewPoint {
val zoom = log2( val zoom = log2(
min( min(
canvasSize.width.value / longitudeDelta.radians.value, canvasSize.width.value / longitudeDelta.radians.value,
canvasSize.height.value / latitudeDelta.radians.value canvasSize.height.value / latitudeDelta.radians.value
) * PI / mapTileProvider.tileSize ) * PI / mapTileProvider.tileSize
) )
MapViewPoint(center, zoom) return MapViewPoint(center, zoom)
} }
/**
* Draw a map using convenient parameters. If neither [initialViewPoint], noe [initialRectangle] is defined,
* use map features to infer view region.
* @param initialViewPoint The view point of the map using center and zoom. Is used if provided
* @param initialRectangle The rectangle to be used for view point computation. Used if [initialViewPoint] is not defined.
* @param buildFeatures - a builder for features
*/
@Composable @Composable
public fun MapView( public fun MapView(
mapTileProvider: MapTileProvider, mapTileProvider: MapTileProvider,
initialViewPoint: MapViewPoint? = null, initialViewPoint: MapViewPoint? = null,
initialRectangle: GmcRectangle? = null,
config: MapViewConfig = MapViewConfig(), config: MapViewConfig = MapViewConfig(),
modifier: Modifier = Modifier.fillMaxSize(), modifier: Modifier = Modifier.fillMaxSize(),
buildFeatures: @Composable (MapFeatureBuilder.() -> Unit) = {}, buildFeatures: @Composable (MapFeatureBuilder.() -> Unit) = {},
@ -130,15 +139,10 @@ public fun MapView(
MapView( MapView(
mapTileProvider, mapTileProvider,
{ canvasSize -> { canvasSize ->
initialViewPoint ?: features.values.computeBoundingBox(1.0)?.let { box -> initialViewPoint
val zoom = log2( ?: initialRectangle?.computeViewPoint(mapTileProvider, canvasSize)
min( ?: features.values.computeBoundingBox(1.0)?.computeViewPoint(mapTileProvider, canvasSize)
canvasSize.width.value / box.longitudeDelta.radians.value, ?: MapViewPoint(GeodeticMapCoordinates(0.0.radians, 0.0.radians), 1.0)
canvasSize.height.value / box.latitudeDelta.radians.value
) * PI / mapTileProvider.tileSize
)
MapViewPoint(box.center, zoom)
} ?: MapViewPoint(GeodeticMapCoordinates(0.0.radians, 0.0.radians), 1.0)
}, },
features, features,
newConfig, newConfig,

View File

@ -158,7 +158,7 @@ public actual fun MapView(
) )
config.onSelect(gmcBox) config.onSelect(gmcBox)
if (config.zoomOnSelect) { if (config.zoomOnSelect) {
val newViewPoint = gmcBox.computeViewPoint(mapTileProvider).invoke(canvasSize) val newViewPoint = gmcBox.computeViewPoint(mapTileProvider, canvasSize)
config.onViewChange(newViewPoint) config.onViewChange(newViewPoint)
viewPoint = newViewPoint viewPoint = newViewPoint