learning_cesium/加载WFS面要素label标签.html

222 lines
9.1 KiB
HTML
Raw Permalink Normal View History

2024-03-19 18:06:25 +08:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script src="./Build/Cesium/Cesium.js"></script>
<script src="./js/turf.min.js"></script>
<link href="./Build/Cesium/Widgets/widgets.css" rel="stylesheet" />
<!-- 添加罗盘 -->
<script>
var defineProperties = Object.defineProperties;
</script>
<script src="./js/nav/CesiumNavigation.umd.js"></script>
<link href="./js/nav/cesium-navigation.css" rel="stylesheet" />
<style>
@import url(./Build/Cesium/Widgets/widgets.css);
html,
body,
#cesiumContainer {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="cesiumContainer"></div>
<script>
Cesium.Ion.defaultAccessToken =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI5ZjRjNTZkNC01NDYxLTRhMjQtOGEwZC1kZjA3YzQ5YTJlZDkiLCJpZCI6MjYwODQsInNjb3BlcyI6WyJhc3IiLCJnYyJdLCJpYXQiOjE1ODcxOTMwODN9.prGsSKyAW_9Ow5zHYPhbm3LsQL-ApQw5-5PNJkDaHi8";
var viewer = new Cesium.Viewer("cesiumContainer", {
// terrainProvider: Cesium.createWorldTerrain({
// requestVertexNormal: true, //添加地形光照
// requestWaterMask: true, //添加水面波浪效果
// }),
// baseLayerPicker: false, //底图选择器
// mapProjection : new Cesium.WebMercatorProjection(),
// geocoder:false,//位置查找
// homeButton:false,//视图返回初始位置
// sceneModePicker:false,//视角选择器
// navigationHelpButton:false,//导航帮助按钮
// animation:false,//动画控制器
// // creditContainer:"credit",//版权显示,指定dom对象的id,再通过id样式可以隐藏CESIUM 版本图标
// timeline:false,//时间线
// fullscreenButton:false,//全屏控件
// vrButton:false,
// infoBox:true ,
// shouldAnimate:true,
// scene3DOnly:true,//默认false若为true,所有几何体实例将仅会在3D模式中渲染(在GPU内存中)
// terrainProvider: Cesium.createWorldTerrain({
// requestVertexNormal: true, //添加地形光照
// requestWaterMask: true, //添加水面波浪效果
// }),
});
viewer.imageryLayers.removeAll();
// 添加 Mapbox tile provider
const mapboxAccess =
"pk.eyJ1IjoicWl1c2hpamllIiwiYSI6ImNsMDNvdDRybDEyc2YzZG9kbWZoY2FuOW0ifQ.4FH-BUupi46Z0zQ-CEm_Ig";
// 样式id查看https://docs.mapbox.com/api/maps/styles/
// URL格式的 mapbox://styles/:owner/:style 其中 :owner 是您的 Mapbox 账户名 :style 是样式 ID 。
// 可选值streets-v10、outdoors-v10 、light-v9、dark-v9、satellite-v9、satellite-streets-v10
const mapboxStyleImagery = new Cesium.MapboxStyleImageryProvider({
styleId: "dark-v9",
accessToken: mapboxAccess,
// proxy: new Cesium.DefaultProxy("http://127.0.0.1:8080/proxy/"),
});
viewer.imageryLayers.addImageryProvider(mapboxStyleImagery);
var options = {};
// 用于在使用重置导航重置地图视图时设置默认视图控制。接受的值是Cesium.Cartographic 和 Cesium.Rectangle.
options.defaultResetView = Cesium.Rectangle.fromDegrees(80, 22, 130, 50);
// 用于启用或禁用罗盘。true是启用罗盘false是禁用罗盘。默认值为true。如果将选项设置为false则罗盘将不会添加到地图中。
options.enableCompass = true;
// 用于启用或禁用缩放控件。true是启用false是禁用。默认值为true。如果将选项设置为false则缩放控件将不会添加到地图中。
options.enableZoomControls = true;
// 用于启用或禁用距离图例。true是启用false是禁用。默认值为true。如果将选项设置为false距离图例将不会添加到地图中。
options.enableDistanceLegend = true;
// 用于启用或禁用指南针外环。true是启用false是禁用。默认值为true。如果将选项设置为false则该环将可见但无效。
options.enableCompassOuterRing = true;
CesiumNavigation.umd(viewer, options);
var scene = viewer.scene;
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(
114.89555032,
32.40536756,
4000
),
orientation: {
heading: Cesium.Math.toRadians(0),
pitch: Cesium.Math.toRadians(-90),
roll: Cesium.Math.toRadians(0),
},
});
// 加载淮河水系面
let url =
"http://localhost:8088/geoserver/allRegion/ows?srsname=EPSG:4326&service=WFS&version=1.0.0&request=GetFeature&typeName=allRegion%3Ajiguan&maxFeatures=50&outputFormat=application%2Fjson";
fetch(url)
.then((res) => res.json())
.then((result) => {
const primitives = new Cesium.PrimitiveCollection();
const labelCollection = new Cesium.LabelCollection();
let colorOption = [0.0, 255, 0.0, 0.8];
let color = new Cesium.ColorGeometryInstanceAttribute.fromColor(
Cesium.Color.fromBytes(
colorOption[0],
colorOption[1],
colorOption[2],
colorOption[3]
)
);
result.features.forEach((feature) => {
// 添加面要素
primitives.add(createPrimitiveByFeature1(feature, color));
// 添加label
labelCollection.add(addLabel(feature));
});
viewer.scene.primitives.add(primitives);
viewer.scene.primitives.add(labelCollection);
// window.labelCollection = labelCollection
});
function addLabel(feature) {
// 计算中心点
let polygon = feature.geometry.coordinates[0];
let points = [];
polygon.forEach((ring) => {
ring.forEach((coord) => {
points.push([coord[0], coord[1]]);
});
});
let pointss = turf.points(points);
let centerResult = turf.center(pointss);
// console.log(centerResult)
let center = centerResult.geometry.coordinates;
let height = 30;
// addLine(center,height);
return {
position: Cesium.Cartesian3.fromDegrees(center[0], center[1], height),
text: feature.properties["名称"]
? feature.properties["名称"]
: "null",
font: "24px Helvetica",
horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // 水平对齐方式
// verticalOrigin:Cesium.HorizontalOrigin.CENTER,// 垂直对齐方式
fillColor: Cesium.Color.WHITE,
outlineColor: Cesium.Color.BLACK,
outlineWidth: 1,
showBackground: true,
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
scale: 0.8,
disableDepthTestDistance: 100.0,
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
10.0,
30000.0
), //根据到相机的距离确定可见性。
scaleByDistance: new Cesium.NearFarScalar(500, 1, 30000, 0.0),
// translucencyByDistance: new Cesium.NearFarScalar(500, 1, 1400, 0.0)
};
}
function addLine(center, height) {
scene.primitives.add(
new Cesium.Primitive({
geometryInstances: new Cesium.GeometryInstance({
geometry: new Cesium.PolylineGeometry({
positions: Cesium.Cartesian3.fromDegreesArrayHeights([
center[0],
center[1],
0,
center[0],
center[1],
height,
]),
width: 2.0,
arcType: Cesium.ArcType.RHUMB, //ArcType. GEODESIC NONE RHUMB
// colors:[Cesium.Color.VIOLET,Cesium.Color.WHEAT,Cesium.Color.THISTLE],//指定线段每段的颜色,数量必须与线段数相同
}),
attributes: {
// 通过attributes属性统一指定线段颜色
color: Cesium.ColorGeometryInstanceAttribute.fromColor(
new Cesium.Color(44 / 255, 44 / 255, 44 / 255, 0.2)
),
},
}),
appearance: new Cesium.PolylineColorAppearance({
translucent: false,
}),
})
);
}
function createPrimitiveByFeature1(feature, color) {
let coords = feature.geometry.coordinates;
let ground = new Cesium.GroundPrimitive({
geometryInstances: new Cesium.GeometryInstance({
geometry: new Cesium.PolygonGeometry({
polygonHierarchy: new Cesium.PolygonHierarchy(
Cesium.Cartesian3.fromDegreesArrayHeights(coords.flat(3))
),
}),
id: feature.properties,
attributes: {
color: Cesium.ColorGeometryInstanceAttribute.fromColor(
new Cesium.Color(1.0, 0, 1.0, 1.0)
),
},
}),
appearance: new Cesium.PerInstanceColorAppearance({
translucent: false,
}),
});
return ground;
}
</script>
</body>
</html>