Untitled

 avatar
unknown
plain_text
5 days ago
9.6 kB
3
Indexable
class MainScreen {
    var calloutLocation = mutableStateOf<Point?>(null)
    MapView(
                modifier = Modifier.fillMaxSize(),
                mapViewProxy = mapViewProxy,
                arcGISMap = map,
                graphicsOverlays = listOf(graphicsOverlay),
                locationDisplay = locationDisplay,
                onSingleTapConfirmed = { screenCoordinate ->
                    if (isMeasuring.value) {
                        val mapPoint: Point? = mapViewProxy.screenToLocationOrNull(screenCoordinate.screenCoordinate)

                        if (mapPoint != null) {
                            pointsDrawn = true
                            val mapPointNotNull: Point = mapPoint
                            val mapPointProjected: Point? = GeometryEngine.projectOrNull(
                                mapPointNotNull,
                                SpatialReference.wgs84()
                            )
                            val latitude = mapPointProjected?.y
                            val longitude = mapPointProjected?.x

                            calloutLocation.value = mapPointProjected

                            Toast.makeText(
                                context,
                                "Map tapped at: Latitude: $latitude, Longitude: $longitude",
                                Toast.LENGTH_SHORT
                            ).show()

                            val relatedGraphics = mutableListOf<Graphic>()

                            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
                                            )

                                            val centroid = GeometryEngine.labelPointOrNull(polygon)
                                            calloutLocation.value = centroid
//
                                            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.value?.let { location ->

                    Callout(location = location, offset = Offset(0f, -50f)) {
                        Text(
                            text = if (selectedGeometryType == "Area") {

                                "${areaText.value} ${originalAreaUnit}"

                            } else {
                                "${distanceText.value} ${originalDistanceUnit}"
//                                distanceText.value
                            }
                        )
                    }
                }
            }private fun undoLastGraphic() {


        if (graphicsStack.isNotEmpty()) {
            val lastGraphics = graphicsStack.removeAt(graphicsStack.size - 1)
            lastGraphics.forEach { graphic ->
                graphicsOverlay.graphics.remove(graphic)
            }

            val selectedGeometryType = "Distance"
            if (selectedGeometryType == "Area" && polygonPoints.isNotEmpty()) {
                polygonPoints.removeAt(polygonPoints.size - 1)
                if (polygonPoints.size < 3) {
                    // If less than 3 points, remove polygon & callout
                     calloutLocation.value = null
                    areaText.value = ""
                } else {
                    // Recalculate area after removing a point
                    convertToArea(polygonSymbol, originalAreaUnit, polygonPoints)
                    calloutLocation.value = polygonPoints.lastOrNull()
                }
            }
            if (selectedGeometryType == "Area" && polygonPoints.isNotEmpty()) {
                polygonPoints.removeAt(polygonPoints.size - 1)
                if (polygonPoints.size < 3) {
                    // If less than 3 points, remove polygon & callout
                     calloutLocation.value = null
                    areaText.value = ""
                } else {
                    // Recalculate area after removing a point
                    convertToArea(polygonSymbol, originalAreaUnit, polygonPoints)
                     calloutLocation.value = polygonPoints.lastOrNull()
                }
            }


        }
    }
 if (pointsDrawn) {
                    IconButton(onClick = { undoLastGraphic() }) {
                        Image(
                            painter = painterResource(R.mipmap.ic_undo),
                            contentDescription = "Undo"
                        )
                    }
Editor is loading...
Leave a Comment