distance measurer

This commit is contained in:
Alexey 2025-09-30 13:27:56 +03:00
commit 049311ec9b

View file

@ -49,6 +49,7 @@ fun MapCanvas(
var pointLat by rememberSaveable { viewModel.rememberedPointLat } var pointLat by rememberSaveable { viewModel.rememberedPointLat }
var pointLon by rememberSaveable { viewModel.rememberedPointLon } var pointLon by rememberSaveable { viewModel.rememberedPointLon }
var invokeHeightCalc by remember { mutableStateOf(false) } var invokeHeightCalc by remember { mutableStateOf(false) }
val startTargetMeters = 64 * 1000 * 1000
Canvas( Canvas(
modifier = modifier.fillMaxSize() modifier = modifier.fillMaxSize()
.pointerInput(Unit) { .pointerInput(Unit) {
@ -93,6 +94,10 @@ fun MapCanvas(
} }
} }
val targetMeters = startTargetMeters shr level
val targetPixels = (targetMeters).toFloat() / SphereMercator.scaledMetersPerPixel(level)
val measurerHeight = 24F
val centerTileX = (1 + (offsetX + halvedX) / TILE_SIZE).toDouble() val centerTileX = (1 + (offsetX + halvedX) / TILE_SIZE).toDouble()
val centerTileY = (1 + (offsetY + halvedY) / TILE_SIZE).toDouble() val centerTileY = (1 + (offsetY + halvedY) / TILE_SIZE).toDouble()
@ -239,19 +244,31 @@ fun MapCanvas(
} }
// Cursor path // Cursor path
val path = Path() val cursorPath = Path()
path.moveTo(halvedX - crossRadius, halvedY) with(cursorPath) {
path.lineTo(halvedX + crossRadius, halvedY) moveTo(halvedX - crossRadius, halvedY)
path.moveTo(halvedX, halvedY - crossRadius) lineTo(halvedX + crossRadius, halvedY)
path.lineTo(halvedX, halvedY + crossRadius) moveTo(halvedX, halvedY - crossRadius)
path.close() lineTo(halvedX, halvedY + crossRadius)
close()
}
// Cursor // Cursor
drawPath( drawPath(
path, cursorPath,
Color.White, Color.White,
style = Stroke(width = 6F) style = Stroke(width = 6F)
) )
// Height under cursor
drawText(
textMeasurer = textMeasurer,
text = buildAnnotatedString {
withStyle(SpanStyle(color = Color.White)) {
append("${KhmParser.getHeight(lon, lat, ctx)}m")
}
},
topLeft = Offset(halvedX + crossRadius, halvedY + crossRadius)
)
// Info box // Info box
drawRect( drawRect(
@ -277,15 +294,41 @@ fun MapCanvas(
topLeft = latLonOffset topLeft = latLonOffset
) )
// Height under cursor // Distance measurer path
val measurerPath = Path()
with(measurerPath) {
moveTo((halvedX - targetPixels).toFloat(), 8F)
relativeLineTo(0F, measurerHeight)
relativeMoveTo(targetPixels.toFloat() * 2F, 0F)
relativeLineTo(0F, -measurerHeight)
relativeMoveTo(0F, measurerHeight / 2F)
relativeLineTo(-2F * targetPixels.toFloat(), 0F)
close()
}
// Distance measurer
drawPath(
measurerPath,
Color.White,
style = Stroke(width = 6F)
)
// Distance measurer text
drawText( drawText(
textMeasurer = textMeasurer, textMeasurer = textMeasurer,
text = buildAnnotatedString { text = buildAnnotatedString {
withStyle(SpanStyle(color = Color.White)) { withStyle(ParagraphStyle(textAlign = TextAlign.Center)) {
append("${KhmParser.getHeight(lon, lat, ctx)}m") withStyle(SpanStyle(color = Color.White)) {
val text = if (targetMeters >= 100000)
"${targetMeters / 1000}km"
else
"${targetMeters}m"
append(text)
}
} }
}, },
topLeft = Offset(halvedX + crossRadius, halvedY + crossRadius) topLeft = Offset(targetPixels.toFloat() * 2F, measurerHeight),
size = Size(targetPixels.toFloat() * 2F, 48F)
) )
} }
} }