Untitled

 avatar
unknown
plain_text
16 days ago
13 kB
4
Indexable
                            calloutLocation = 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
                                            )
//                                            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 if (selectedGeometryType == "Distance"){
                                "${distanceText.value} ${originalDistanceUnit}"
//                                distanceText.value
                            } else {
                                ""
                            }
                        )
                    }
                }
            }


            ImageButtonWithSpinner(mapType, context)

            IconButton(
                onClick = {
                    if (checkPermissions(context)) {
                        coroutineScope.launch {
                            locationDisplay.setAutoPanMode(LocationDisplayAutoPanMode.Navigation)
                            locationDisplay.dataSource.start()
                        }
                    } else {
                        requestPermissions = true
                    }
                },
                modifier = Modifier
                    .align(Alignment.CenterEnd)
                    .padding(end = 16.dp, top = 200.dp)
            ) {
                Image(
                    painter = painterResource(R.mipmap.ic_location),
                    contentDescription = "My Location"
                )
            }

            IconButton(
                onClick = {
                    showActionBar.value = true
                    isMeasuring.value = true
                },
                modifier = Modifier
                    .align(Alignment.CenterEnd)
                    .padding(end = 16.dp, top = 40.dp)
            ) {
                Image(
                    painter = painterResource(R.mipmap.ic_measure),
                    contentDescription = "Measure"
                )
            }

            if (showActionBar.value) {
                CustomTopBar(
                    selectedGeometryType = selectedGeometryType,
                    onGeometryTypeChange = { selectedGeometryType = it },
                    selectedUnit = selectedUnit,
//                    onUnitChange = { selectedUnit = it },
                    onUnitChange = { unit ->
                        selectedUnit = unit
//                        if (selectedGeometryType == "Distance") {
//                            updateDistanceUnits(unit)
//                        } else if (selectedGeometryType == "Area") {
//                            updateAreaUnits(originalArea, unit)
//                        }
                    },
                    distanceUnits = distanceUnits,
                    areaUnits = areaUnits,
                    onShowActionBarChange = { showActionBar.value = it },
                    onClearGraphics = {
                        clearGraphics()
                        linePoints.clear()
                        polygonPoints.clear()
                        calloutLocation = null // Reset callout location
                        distanceText.value = " " // Reset distance text
//                        distanceTextState.value = " "
                        areaText.value = " " // Reset area text
                        cumulativeDistance = 0.0 // Reset cumulative distance
                    },
                    pointsDrawn = pointsDrawn

                )
            }


            // Call setupMapClickListener here
            setupMapClickListener()
        }

    }



    private fun getAreaUnit(unit: String): AreaUnit? {
        return when (unit) {
            "Square Meter" -> AreaUnit.squareMeters
            "Square Kilometer" -> AreaUnit.squareKilometers
            "Acre" -> AreaUnit(AreaUnitId.Acres)
            "Hectare" -> AreaUnit(AreaUnitId.Hectares)
            "Square Foot" -> AreaUnit.squareFeet
            else -> null
        }
    }

    private fun getLinearUnit(unit: String): LinearUnit? {
        return when (unit) {
            "Meter" -> LinearUnit.meters
            "Kilometre" -> LinearUnit.kilometers
            "Foot" -> LinearUnit.feet
            "Yard" -> LinearUnit(LinearUnitId.Yards)
            else -> null
        }
    }

    private fun formatMeasurementValue(value: Double, unit: String): String {
        var measureVal:String =  String.format("%.2f", value)
        var mutablemeasureVal = mutableStateOf(measureVal)
        return mutablemeasureVal.value
//        return "$value $unit"
    }

    private fun clearGraphics() {
        graphicsOverlay.graphics.clear()
        graphicsStack.clear()
//        linePoints.clear()
//        polygonPoints.clear()
    }
    private var calloutLocation: Location? = null
    private var selectedGeometryType: String? = null


    private fun undoLastGraphic() {



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

        }
    }


    @Composable
    fun CustomTopBar(
        selectedGeometryType: String,
        onGeometryTypeChange: (String) -> Unit,
        selectedUnit: String,
        onUnitChange: (String) -> Unit,
        distanceUnits: List<String>,
        areaUnits: List<String>,
        onShowActionBarChange: (Boolean) -> Unit,
        onClearGraphics: () -> Unit,
        pointsDrawn: Boolean


    ) {
        var expanded by remember { mutableStateOf(false) }
        val units = if (selectedGeometryType == "Distance") distanceUnits else areaUnits

        Box(
            modifier = Modifier
                .fillMaxWidth()
                .padding(8.dp)
                .background(color = Color.DarkGray)
        ) {
            Row(
                modifier = Modifier.align(Alignment.CenterStart),
                verticalAlignment = Alignment.CenterVertically
            ) {

                IconButton(onClick = { onShowActionBarChange(false)
                    onClearGraphics()
                    isMeasuring.value = false
                }) {
                    Image(
                        painter = painterResource(R.mipmap.ic_backbutton),
                        contentDescription = "Back"
                    )
                }
            }
            Row(
                modifier = Modifier.align(Alignment.CenterEnd),
                verticalAlignment = Alignment.CenterVertically
            ) {

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