Untitled
unknown
plain_text
8 months ago
25 kB
4
Indexable
private val graphicsStack = mutableListOf<List<Graphic>>()
private val linePoints = mutableListOf<Point>()
private val polygonPoints = mutableListOf<Point>()
private val isMeasuring = mutableStateOf(false)
private val showActionBar = mutableStateOf(false)
var distance:Double = 0.0
var area:Double = 0.0
var cumulativeDistance = 0.0
var originalArea: Double = 0.0
var originalDistanceUnit: String = "Meter"
var originalAreaUnit: String = "Acre"
var distanceText:MutableState<String> = mutableStateOf("0.0")
var areaText:MutableState<String> = mutableStateOf("0.0")
var calloutLocation = mutableStateOf<Point?>(null)
var selectedGeometryType by mutableStateOf("Distance")
// var distanceTextState: MutableLiveData<String> = MutableLiveData() // commented by Ramya
@SuppressLint("SuspiciousIndentation")
@Composable
fun MainScreenComposable() {
val context = LocalContext.current
val coroutineScope = rememberCoroutineScope()
ArcGISEnvironment.applicationContext = context.applicationContext
val mapType: MutableState<BasemapStyle> = remember { mutableStateOf(BasemapStyle.ArcGISTopographic) }
lifeCycleOwner = LocalLifecycleOwner.current
val locationDisplay = rememberLocationDisplay().apply {
setAutoPanMode(LocationDisplayAutoPanMode.Recenter)
}
val map = remember(mapType.value) {
ArcGISMap(mapType.value).apply {
initialViewpoint = Viewpoint(
latitude = 13.0236, longitude = 80.1780, scale = 72000.0
)
}
}
// var cumulativeDistance by remember { mutableStateOf(0.0) }
var requestPermissions by remember { mutableStateOf(false) }
var selectedUnit by remember { mutableStateOf("Meter") }
var pointsDrawn by remember { mutableStateOf(false) }
val distanceUnits = mutableListOf("Meter", "Kilometre", "Foot", "Yard")
val areaUnits = listOf("Acre", "Hectare", "Square Meter", "Square Kilometer", "Square Foot")
if (selectedGeometryType == "Area" && selectedUnit !in areaUnits) {
selectedUnit = "Acre"
}
if (requestPermissions) {
RequestPermissions(
onPermissionGranted = {
coroutineScope.launch {
locationDisplay.setAutoPanMode(LocationDisplayAutoPanMode.Navigation)
locationDisplay.dataSource.start()
}
}
)
}
Box(modifier = Modifier.fillMaxSize()) {
val mapViewProxy = MapViewProxy()
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
}
)
}
}
}
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 = { onGeometryTypeChange(it) },
selectedUnit = selectedUnit,
// onUnitChange = { selectedUnit = it },
onUnitChange = { unit ->
selectedUnit = unit
if (selectedGeometryType == "Distance") {
if (areaUnits.contains(unit) && linePoints.size >= 3) {
onGeometryTypeChange("Area")
polygonPoints.clear()
polygonPoints.addAll(linePoints)
// Convert to Area with the correct unit
convertToArea(polygonSymbol, unit, polygonPoints.toList(), graphicsOverlay, areaText)
} else {
updateDistanceUnits(unit)
}
} else if (selectedGeometryType == "Area") {
updateAreaUnits(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.value = null // Reset callout location
distanceText.value = " " // Reset distance text
// distanceTextState.value = " "
areaText.value = " " // Reset area text
cumulativeDistance = 0.0 // Reset cumulative distance
},
pointsDrawn = pointsDrawn,
polygonSymbol = polygonSymbol
)
}
// Call setupMapClickListener here
setupMapClickListener()
}
}
private fun onGeometryTypeChange(newType: String) {
selectedGeometryType = newType
}
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 val distanceStack = mutableListOf<Double>()
private fun undoLastGraphic() {
if (graphicsStack.isNotEmpty()) {
val lastGraphics = graphicsStack.removeAt(graphicsStack.size - 1)
lastGraphics.forEach { graphic ->
graphicsOverlay.graphics.remove(graphic)
}
if (selectedGeometryType == "Area" && polygonPoints.isNotEmpty()) {
polygonPoints.removeAt(polygonPoints.size - 1)
if (polygonPoints.size < 3) {
calloutLocation.value = null
areaText.value = ""
} else {
convertToArea(polygonSymbol, originalAreaUnit, polygonPoints, graphicsOverlay, areaText)
val polygon = Polygon(polygonPoints)
calloutLocation.value = GeometryEngine.labelPointOrNull(polygon)
}
}
if (selectedGeometryType == "Distance" && linePoints.isNotEmpty()) {
linePoints.removeAt(linePoints.size - 1)
if (linePoints.size < 2) {
calloutLocation.value = null
distanceText.value = ""
cumulativeDistance = 0.0
graphicsOverlay.graphics.removeAll{ it.symbol is TextSymbol}
} else {
// Recalculate the cumulative distance after removing the last point
cumulativeDistance = 0.0
for (i in 1 until linePoints.size) {
val segment = Polyline(listOf(linePoints[i - 1], linePoints[i]))
val segmentLength = GeometryEngine.lengthGeodetic(
segment,
getLinearUnit(originalDistanceUnit),
GeodeticCurveType.Geodesic
)
// Log.d("tg", "undoLastGraphic: "+linePoints)
// Log.d("tg", "undoLastGraphic: "+segment +""+segmentLength)
cumulativeDistance += segmentLength
// println("Segment $i: ${linePoints[i - 1]} to ${linePoints[i]}, Length: $segmentLength")
}
distanceText.value = formatMeasurementValue(cumulativeDistance, originalDistanceUnit)
calloutLocation.value = linePoints.lastOrNull()
graphicsOverlay.graphics.removeAll{ it.symbol is TextSymbol}
}
}
if (graphicsOverlay.graphics.isEmpty()) {
calloutLocation.value = 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,
polygonSymbol : Symbol?
) {
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"
)
}
IconButton(onClick = {
onClearGraphics()
// linePoints.clear()
// polygonPoints.clear()
}) {
Image(
painter = painterResource(R.mipmap.ic_delete),
contentDescription = "Delete"
)
}
}
IconButton(onClick = { expanded = true }) {
Image(
painter = painterResource(R.mipmap.ic_selection),
contentDescription = "Settings"
)
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
modifier = Modifier.width(150.dp),
) {
Column(modifier = Modifier.padding(1.dp)) {
Text(text = "Select Geometry Type", color = Color.Blue)
Row(verticalAlignment = Alignment.CenterVertically) {
Checkbox(
checked = selectedGeometryType == "Distance",
onCheckedChange = {
if (it) onGeometryTypeChange("Distance")
}
)
Text(text = "Distance")
}
Row(verticalAlignment = Alignment.CenterVertically) {
Checkbox(
checked = selectedGeometryType == "Area",
onCheckedChange = {
if (it) onGeometryTypeChange("Area")
}
)
Text(text = "Area")
}
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")
polygonPoints.clear()
polygonPoints.addAll(linePoints)
convertToArea(polygonSymbol, unit, linePoints.toList(), graphicsOverlay, areaText)
}
else {
println("Conditions not met for converting to Area")
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>, graphicsOverlay: GraphicsOverlay, areaText: MutableState<String>) {
if (points.size < 3 || polygonSymbol == null) {
return
}
val closedPoints = points.toMutableList().apply { add(points.first()) }
val polygon = Polygon(closedPoints)
graphicsOverlay.graphics.removeIf { it.geometry is Polyline }
graphicsOverlay.graphics.removeIf { it.geometry is Polygon }
val polygonGraphic = Graphic(polygon, polygonSymbol)
graphicsOverlay.graphics.add(polygonGraphic)
val areaValue = GeometryEngine.areaGeodetic(
polygon,
getAreaUnit(selectedUnit),
GeodeticCurveType.Geodesic
)
// originalArea = areaValue
// originalAreaUnit = selectedUnit
areaText.value = formatMeasurementValue(areaValue, selectedUnit)
}Editor is loading...
Leave a Comment