Files
icegps-geotools/android/src/main/java/com/icegps/geotools/TriangleDisplay.kt
tabidachinokaze af2257b467
Some checks failed
Build and generate screenshots / generate_screenshots (push) Has been cancelled
Release API docs / release_apidocs (push) Has been cancelled
feat: 添加当前位置Marker,显示实时设计高度,完善地形图代码
2025-11-28 22:40:10 +08:00

54 lines
1.6 KiB
Kotlin

package com.icegps.geotools
import com.icegps.geotools.ktx.toMapboxPoint
import com.icegps.triangulation.Triangle
import com.mapbox.geojson.Feature
import com.mapbox.geojson.FeatureCollection
import com.mapbox.geojson.LineString
import com.mapbox.maps.MapView
import com.mapbox.maps.extension.style.layers.addLayer
import com.mapbox.maps.extension.style.layers.generated.lineLayer
import com.mapbox.maps.extension.style.layers.properties.generated.LineJoin
import com.mapbox.maps.extension.style.sources.addSource
import com.mapbox.maps.extension.style.sources.generated.geoJsonSource
/**
* @author tabidachinokaze
* @date 2025/11/28
*/
fun MapView.displayTriangle(
triangles: List<Triangle>,
sourceId: String,
layerId: String
) {
val features = triangles.map {
listOf(it.x1, it.x2, it.x3)
}.map {
fromPoints(points = it, closed = true)
}.map {
it.map { line ->
val lineA = line.a.toMapboxPoint()
val lineB = line.b.toMapboxPoint()
LineString.fromLngLats(listOf(lineA, lineB))
}.map {
Feature.fromGeometry(it)
}
}.flatten()
mapboxMap.getStyle { style ->
style.removeStyleLayer(layerId)
style.removeStyleSource(sourceId)
val source = geoJsonSource(sourceId) {
featureCollection(FeatureCollection.fromFeatures(features))
}
style.addSource(source)
val layer = lineLayer(layerId, sourceId) {
lineWidth(1.5)
lineJoin(LineJoin.ROUND)
lineOpacity(1.0)
lineColor("#ff0000")
}
style.addLayer(layer)
}
}