Untitled
Anonymous
plain_text
03/11/2025 5:15 AM
12.5 KB
11
Indexable
when (selectedGeometryType) {
"Area" -> {
val pointGraphic = Graphic(mapPoint, pointSymbol)
graphicsOverlay.graphics.add(pointGraphic)
relatedGraphics.add(pointGraphic)
polygonPoints.add(mapPointNotNull)
if (polygonPoints.size > 1) {
val polyline = Polyline(polygonPoints)
val lineGraphic = Graphic(polyline, lineSymbol)
graphicsOverlay.graphics.add(lineGraphic)
relatedGraphics.add(lineGraphic)
// Remove previous text symbols
graphicsOverlay.graphics.removeAll { it.symbol is TextSymbol }
if (polygonPoints.size > 2) {
val polygon = Polygon(polygonPoints)
val polygonGraphic = Graphic(polygon, polygonSymbol)
graphicsOverlay.graphics.add(polygonGraphic)
relatedGraphics.add(polygonGraphic)
val areaValue = GeometryEngine.areaGeodetic(
polygon,
getAreaUnit(selectedUnit),
GeodeticCurveType.Geodesic
)
// if (originalArea == 0.0){
// originalArea = areaValue
// originalAreaUnit = selectedUnit
// }
area = areaValue
// originalAreaUnit = selectedUnit
areaText.value = formatMeasurementValue(areaValue, selectedUnit) // Update area text
} else {
val distanceValue = GeometryEngine.lengthGeodetic(
polyline,
getLinearUnit(selectedUnit),
GeodeticCurveType.Geodesic
)
distance = distanceValue
distanceText.value = formatMeasurementValue(distanceValue, selectedUnit) // Update area text
// distanceTextState.postValue(distanceValue.toString())
}
}
}
"Distance" -> {
val pointGraphic = Graphic(mapPointNotNull, pointSymbol)
graphicsOverlay.graphics.add(pointGraphic)
relatedGraphics.add(pointGraphic)
linePoints.add(mapPointNotNull)
if (linePoints.size > 1) {
val polyline = Polyline(linePoints)
val lineGraphic = Graphic(polyline, lineSymbol)
graphicsOverlay.graphics.add(lineGraphic)
relatedGraphics.add(lineGraphic)
val distanceValue = GeometryEngine.lengthGeodetic(
polyline,
getLinearUnit(selectedUnit),
GeodeticCurveType.Geodesic
)
cumulativeDistance += distanceValue
}
// Remove previous text symbols
graphicsOverlay.graphics.removeAll { it.symbol is TextSymbol }
// Only add the text symbol at the last point
if (linePoints.size > 1) {
linePoints.last()
distance = cumulativeDistance
distanceText.value = formatMeasurementValue(cumulativeDistance, selectedUnit)
// distanceTextState.value = distanceText.value
TextSymbol().apply {
text = distanceText.value
color = com.arcgismaps.Color.fromRgba(0, 0, 0, 255) // Black color
size = 15f
}
}
}
}
graphicsStack.add(relatedGraphics)
}
}
}
)
{
calloutLocation?.let { location ->
Callout(location = location, offset = Offset(0f, -50f)) {
Text(
text = if (selectedGeometryType == "Area") {
"${areaText.value} ${originalAreaUnit}"
} else {
"${distanceText.value} ${originalDistanceUnit}"
// distanceText.value
}
)
}
}
Text(text = "Select Unit", color = Color.Blue)
units.forEach { unit ->
Row(verticalAlignment = Alignment.CenterVertically) {
Checkbox(
checked = selectedUnit == unit,
onCheckedChange = {
if (it) {
onUnitChange(unit)
if (selectedGeometryType == "Distance") {
updateDistanceUnits(unit)
} else if (selectedGeometryType == "Area") {
updateAreaUnits(unit)
}
}
}
)
Text(text = unit, modifier = Modifier.padding(4.dp))
}
}
}
}
}
}
}
private fun updateDistanceUnits(unit: String) {
distance = when (unit) {
"Meter" -> {
if (originalDistanceUnit.equals("Kilometre")){
convertFromKiloMeterToMeter(distance)
}
else if (originalDistanceUnit.equals("Foot")){
convertFromFootToMeter(distance)
}
else {
convertFromYardToMeter(distance)
}
} }
else -> distance
}
originalDistanceUnit = unit
// distanceTextState.value = "%.2f" .format(distance)
distanceText.value = "%.2f" .format(distance)
}
private fun convertFromMeterToKiloMeter(distance: Double):Double{
return distance / 1000
}
private fun convertFromMeterToFeet(distance: Double): Double {
return distance * 3.28084
}
private fun convertFromMeterToYards(distance: Double): Double {
return distance * 1.09361
}private fun updateAreaUnits( unit: String) {
// if (originalArea == 0.0) return
area = when (unit) {
"Acre" -> {
if (originalAreaUnit.equals("Hectare")){
convertHectareToAcre(area)
}
else if (originalAreaUnit.equals("Square Meter")){
convertSquareMeterToAcre(area)
}
else if (originalAreaUnit.equals("Square Kilometer")){
convertSquareKilometerToAcre(area)
}
else{
convertSquareFootToAcre(area)
}
}
else -> {
area
}
}
originalAreaUnit = unit
// updateAreaText("$area $unit")
areaText.value = "%.2f" .format(area)
}
private fun convertAcreToHectare(area: Double):Double{
return area * 0.404686
}
private fun convertAcreToSquareMeter(area: Double):Double{
return area * 4046.86
// return convertAcreToSquareMeter(area) * 0.0001
}
}Editor is loading...
Leave a Comment