Untitled

 avatar
unknown
plain_text
19 days ago
12 kB
4
Indexable
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
//                                            calloutLocation.value = GeometryEngine.labelPointOrNull(polygon)

                                        } 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
                            }
                        )
                    }
                }
            }LaunchedEffect(Unit) {
            withContext(Dispatchers.IO) {
                val geometryTableList: List<GeometryTable> = geometryDao.getAllGeometries()
                for (geometry in geometryTableList) {
                    val geom = Converters().toGeometry(geometry.geometry)
                    val objectId = geometry.objectId
                    val attributeData = attributeTableViewModel.getAttributeForObjectIdVM(objectId)
                    val attributeMap = mutableMapOf<String, Any>()

                    attributeData?.let {
                        attributeMap["OBJECTID"] = it.sobjectId
                        attributeMap["Pipe_ID"] = it.pipeId
                        attributeMap["Status"] = it.status
                        attributeMap["Start_Node_ID"] = it.startNodeID
                        attributeMap["Stop_Node_ID"] = it.stopNodeID
                        attributeMap["Material"] = it.material
                        attributeMap["Design_Dia"] = it.designDia
                        attributeMap["Design_Length"] = it.designLength
                        attributeMap["ZONE"] = it.zone
                    }

                    val polyline = geom as Polyline
                    val lineSymbol =
                        SimpleLineSymbol(SimpleLineSymbolStyle.Solid, com.arcgismaps.Color.red, 10f)
                    val graphic = Graphic(polyline, attributeMap, lineSymbol)
                    graphicsOverlay.graphics.add(graphic)
                }
            }
        }MapView(
//            modifier = Modifier.fillMaxSize(),
//            mapViewProxy = mapViewProxy,
//            arcGISMap = map,
//            graphicsOverlays = listOf(graphicsOverlay),
//            locationDisplay = locationDisplay,

//                coroutineScope.launch {
//                    try {
//
//                        val result = mapViewProxy.mapView?.identifyGraphicsOverlay(
//                            graphicsOverlay,
//                            screenCoordinate.screenCoordinate,
//                            10.0,
//                            false
//                        )?.getOrThrow()
//
//                        val graphic = result?.graphics?.firstOrNull()
//
//                        graphic?.let {
//                            val attr = it.attributes
//                            val calloutText = buildString {
//                                appendLine("OBJECTID: ${attr["OBJECTID"]}")
//                                appendLine("Pipe ID: ${attr["Pipe_ID"]}")
//                                appendLine("Status: ${attr["Status"]}")
//                                appendLine("Start Node: ${attr["Start_Node_ID"]}")
//                                appendLine("Stop Node: ${attr["Stop_Node_ID"]}")
//                                appendLine("Material: ${attr["Material"]}")
//                                appendLine("Design Dia: ${attr["Design_Dia"]}")
//                                appendLine("Design Length: ${attr["Design_Length"]}")
//                                appendLine("Zone: ${attr["ZONE"]}")
//                         }
//                            withContext(Dispatchers.Main) {
//                                val calloutView = TextView(context).apply {
//                                    text = calloutText
//                                    setPadding(16, 16, 16, 16)
//                                }
//
//
//                                val location = graphic.geometry?.extent?.center
//                                mapView.callout.show(calloutView, location)  // FIXED: Correct callout usage
//                            }
//
//                        }
//                    } catch (e: Exception) {
//                        Log.e("MapViewTap", "Error: ${e.message}")
//                    }
//                }
Editor is loading...
Leave a Comment