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 {
group = "center.sciprog"
version = "0.1.0-dev-4"
version = "0.1.0-dev-5"
}
ksciencePublish{

View File

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

View File

@ -101,20 +101,29 @@ private fun prepareConfig(initialConfig: MapViewConfig, featureBuilder: MapFeatu
internal fun GmcRectangle.computeViewPoint(
mapTileProvider: MapTileProvider,
): (canvasSize: DpSize) -> MapViewPoint = { canvasSize ->
canvasSize: DpSize,
): MapViewPoint {
val zoom = log2(
min(
canvasSize.width.value / longitudeDelta.radians.value,
canvasSize.height.value / latitudeDelta.radians.value
) * 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
public fun MapView(
mapTileProvider: MapTileProvider,
initialViewPoint: MapViewPoint? = null,
initialRectangle: GmcRectangle? = null,
config: MapViewConfig = MapViewConfig(),
modifier: Modifier = Modifier.fillMaxSize(),
buildFeatures: @Composable (MapFeatureBuilder.() -> Unit) = {},
@ -130,15 +139,10 @@ public fun MapView(
MapView(
mapTileProvider,
{ canvasSize ->
initialViewPoint ?: features.values.computeBoundingBox(1.0)?.let { box ->
val zoom = log2(
min(
canvasSize.width.value / box.longitudeDelta.radians.value,
canvasSize.height.value / box.latitudeDelta.radians.value
) * PI / mapTileProvider.tileSize
)
MapViewPoint(box.center, zoom)
} ?: MapViewPoint(GeodeticMapCoordinates(0.0.radians, 0.0.radians), 1.0)
initialViewPoint
?: initialRectangle?.computeViewPoint(mapTileProvider, canvasSize)
?: features.values.computeBoundingBox(1.0)?.computeViewPoint(mapTileProvider, canvasSize)
?: MapViewPoint(GeodeticMapCoordinates(0.0.radians, 0.0.radians), 1.0)
},
features,
newConfig,

View File

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