Untitled

 avatar
unknown
plain_text
8 days ago
9.2 kB
3
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 = { isChecked ->
                                        if (isChecked) {
//                                            selectedUnit = unit
                                            onUnitChange(unit)

                                            if (selectedGeometryType == "Distance" && areaUnits.contains(unit) &&linePoints.size >= 3){
                                                onGeometryTypeChange("Area")
                                                convertToArea(polygonSymbol, unit, linePoints.toList())
                                            }
                                            if (selectedGeometryType == "Distance") {
                                                updateDistanceUnits(unit)
                                            } else if (selectedGeometryType == "Area") {
                                                updateAreaUnits(unit)
                                            }
                                        }
                                    }
                                )
                                Text(text = unit, modifier = Modifier.padding(4.dp))
                            }
                        }
                    }
                }
            }
        }
    }

    private fun convertToArea(polygonSymbol: Symbol?, selectedUnit: String, points: List<Point>) {
        if (points.size < 3 || polygonSymbol == null) {
            return // Ensure at least 3 points exist before converting to area
        }
        val closedPoints = points.toMutableList().apply { add(points.first()) }

        val polygon = Polygon(closedPoints) // Convert the line points into a polygon
        graphicsOverlay.graphics.removeIf { it.geometry is Polyline }

        val polygonGraphic = Graphic(polygon, polygonSymbol)

        // Remove previous distance-related graphics
        graphicsOverlay.graphics.clear()

        // Add the polygon graphic to the overlay
        graphicsOverlay.graphics.add(polygonGraphic)

        // Calculate the area of the polygon
        val areaValue = GeometryEngine.areaGeodetic(
            polygon,
            getAreaUnit(selectedUnit),
            GeodeticCurveType.Geodesic
        )


//        areaText.value = formatMeasurementValue(areaValue, selectedUnit)
        originalArea = areaValue
        originalAreaUnit = selectedUnit
        updateAreaUnits(selectedUnit)

//        calloutLocation = polygon.extent.center
        areaText.value = formatMeasurementValue(areaValue, selectedUnit)


    }
Editor is loading...
Leave a Comment