我需要一个可以在cesium上可以随意绘制多边形,并且显示绘制出来的多边形的等值面的一个完整的deom代码
时间: 2023-09-06 17:05:14 浏览: 187
基于Cesium+VUE实现的实现纯前端绘制等高线展示的效果功能,完整demo和源代码,代码未加密/未压缩,可直接调用运行
5星 · 资源好评率100%
### 回答1:
以下是一个可以在Cesium上绘制多边形,并显示等值面的示例代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Cesium Polygon and Isolines Demo</title>
<script src="https://cesium.com/downloads/cesiumjs/releases/1.88/Build/Cesium/Cesium.js"></script>
<link href="https://cesium.com/downloads/cesiumjs/releases/1.88/Build/Cesium/Widgets/widgets.css" rel="stylesheet">
<style>
#cesiumContainer {
height: 100%;
margin: 0;
padding: 0;
}
#toolbar {
position: absolute;
top: 10px;
left: 10px;
z-index: 1;
background-color: rgba(255, 255, 255, 0.5);
padding: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<div id="toolbar">
<button id="draw-polygon-btn">Draw Polygon</button>
<button id="calculate-isolines-btn" disabled>Calculate Isolines</button>
</div>
<div id="cesiumContainer"></div>
<script>
// Initialize Cesium viewer
var viewer = new Cesium.Viewer('cesiumContainer');
// Initialize drawing toolbar
var drawPolygonBtn = document.getElementById('draw-polygon-btn');
var calculateIsolinesBtn = document.getElementById('calculate-isolines-btn');
var drawingHandler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
var polygon = null;
function startDrawing() {
polygon = new Cesium.PolygonGraphics({
hierarchy: new Cesium.PolygonHierarchy(),
material: Cesium.Color.RED.withAlpha(0.5)
});
viewer.entities.add({
polygon: polygon
});
drawingHandler.setInputAction(function(event) {
var position = viewer.scene.pickPosition(event.position);
if (Cesium.defined(position)) {
var cartographic = Cesium.Cartographic.fromCartesian(position);
polygon.hierarchy.positions.push(Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, cartographic.height));
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
drawPolygonBtn.disabled = true;
calculateIsolinesBtn.disabled = false;
}
drawPolygonBtn.addEventListener('click', startDrawing);
// Calculate isolines when button is clicked
calculateIsolinesBtn.addEventListener('click', function() {
var positions = polygon.hierarchy.getValue(Cesium.JulianDate.now()).positions;
var cartographicPositions = positions.map(function(position) {
return Cesium.Cartographic.fromCartesian(position);
});
var terrainProvider = new Cesium.CesiumTerrainProvider({
url: 'https://assets.agi.com/stk-terrain/world',
requestVertexNormals: true
});
var terrainData = new Cesium.HeightmapTerrainData({
buffer: new Float32Array(cartographicPositions.length),
width: 1,
height: cartographicPositions.length,
childTileMask: [1],
structure: {
heightScale: 1.0,
heightOffset: 0.0,
elementsPerHeight: 1,
stride: 1,
elementMultiplier: 256.0,
isBigEndian: false
}
});
for (var i = 0; i < cartographicPositions.length; i++) {
terrainData.buffer[i] = cartographicPositions[i].height;
### 回答2:
以下是一个完整的Cesium代码示例,实现在Cesium上绘制多边形,并显示其等值面:
```javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cesium Draw Polygon and Display Isocontour</title>
<script src="https://cesium.com/downloads/cesiumjs/releases/1.78.1/Build/Cesium/Cesium.js"></script>
<style>
#cesiumContainer {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="cesiumContainer"></div>
<script>
var viewer = new Cesium.Viewer("cesiumContainer");
var handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
var positions = [];
handler.setInputAction(function(click) {
var cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
var longitude = Cesium.Math.toDegrees(cartographic.longitude);
var latitude = Cesium.Math.toDegrees(cartographic.latitude);
positions.push(longitude, latitude);
var drawingPolygon = viewer.entities.add({
polygon: {
hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArray(positions)),
material: Cesium.Color.YELLOW.withAlpha(0.5)
}
});
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
handler.setInputAction(function(click) {
var cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
var longitude = Cesium.Math.toDegrees(cartographic.longitude);
var latitude = Cesium.Math.toDegrees(cartographic.latitude);
positions.push(longitude, latitude);
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
handler.setInputAction(function(click) {
var isocontourPolygons = computeIsocontourPolygons(viewer, positions);
for (var i = 0; i < isocontourPolygons.length; i++) {
viewer.entities.add({
polygon: {
hierarchy: isocontourPolygons[i],
material: Cesium.Color.BLUE.withAlpha(0.5)
}
});
}
positions = []; // 清空位置数组
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
// 计算等值面多边形坐标
function computeIsocontourPolygons(viewer, positions) {
// 在这里添加计算等值面多边形的代码,这里只是一个示例
// 等值面的算法是复杂的,通常需要根据具体需求进行定制
return [new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArray(positions))];
}
</script>
</body>
</html>
```
该代码使用了Cesium JavaScript库,并在页面上创建了一个Cesium Viewer实例,用于显示地球场景。使用ScreenSpaceEventHandler监听鼠标事件,左击鼠标可以绘制多边形,右击鼠标可以显示绘制的多边形的等值面。
具体实现中,在左击事件中,获取鼠标点击位置的经纬度,将其存入positions数组中,并在地图上绘制出一条线段作为多边形的边界。在右击事件中,根据positions数组中的坐标计算等值面多边形的坐标,并将其显示在地图上。
请注意,计算等值面多边形的算法很复杂,需要根据具体需求进行定制。本示例中的computeIsocontourPolygons函数只是一个简单示例,具体实现需要根据你的应用场景进行调整。
### 回答3:
在Cesium上实现绘制多边形并显示等值面的代码示例如下:
1. 首先,在HTML文件中引入Cesium库以及其他所需的资源文件:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cesium Draw Polygon</title>
<script src="https://cesium.com/downloads/cesiumjs/releases/1.83/Build/Cesium/Cesium.js"></script>
<style>
#cesiumContainer {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="cesiumContainer"></div>
<script src="app.js"></script>
</body>
</html>
```
2. 在JavaScript文件app.js中编写绘制多边形以及显示等值面的代码:
```javascript
// 创建Cesium Viewer
var viewer = new Cesium.Viewer('cesiumContainer');
// 定义绘制多边形的变量
var drawingMode = false;
var positions = [];
var drawingPolygon;
// 创建Polyline
function createPolyline() {
var polyline = new Cesium.PolylineGraphics();
polyline.material = Cesium.Color.fromCssColorString('#ff0000');
polyline.width = 5;
return polyline;
}
// 创建Polygon
function createPolygon() {
var polygon = new Cesium.PolygonGraphics();
polygon.material = Cesium.Color.fromCssColorString('#ff0000').withAlpha(0.5);
return polygon;
}
// 开始绘制多边形
function startDrawingPolygon() {
if (!drawingMode) {
drawingMode = true;
drawingPolygon = viewer.entities.add({
polygon: createPolygon()
});
viewer.cesiumWidget.screenSpaceEventHandler.setInputAction(drawPolygon, Cesium.ScreenSpaceEventType.LEFT_CLICK);
}
}
// 绘制多边形
function drawPolygon(click) {
var cartesian = viewer.scene.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
if (cartesian) {
var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
var longitude = Cesium.Math.toDegrees(cartographic.longitude);
var latitude = Cesium.Math.toDegrees(cartographic.latitude);
positions.push(longitude, latitude);
drawingPolygon.polygon.hierarchy = new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArray(positions));
}
}
// 停止绘制多边形
function stopDrawingPolygon() {
if (drawingMode) {
drawingMode = false;
viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
viewer.entities.remove(drawingPolygon);
createContour();
positions = [];
}
}
// 创建等值面
function createContour() {
var layer = viewer.scene.frameState.layers.layerQueue[0]; // 获取默认图层
var grid = new Cesium.GridMaterialProperty({
color: Cesium.Color.RED.withAlpha(0.5),
lineCount: new Cesium.Cartesian2(50, 50),
lineThickness: new Cesium.Cartesian2(2.0, 2.0),
lineOffset: new Cesium.Cartesian2(0.0, 0.0),
});
var contour = new Cesium.EllipsoidOccluder();
var positions3D = Cesium.Cartesian3.fromDegreesArray(positions);
var surface = Cesium.PolygonGeometry.createGeometry(new Cesium.PolygonGeometry({
polygonHierarchy: new Cesium.PolygonHierarchy(positions3D)
}));
var instances = new Cesium.GeometryInstance({
geometry: surface,
attributes: {
color: Cesium.ColorGeometryInstanceAttribute.fromColor(new Cesium.Color(1.0, 0.0, 0.0, 1.0))
}
});
layer.getMaterial = function () {
return grid;
};
layer.addImagery(new Cesium.ImageryLayer(new Cesium.Primitive(({
geometryInstances: instances,
appearance: new Cesium.MaterialAppearance({
material: grid
}),
asynchronous: false
}))));
}
// 监听按钮点击事件
var startBtn = document.createElement('button');
startBtn.textContent = '开始绘制多边形';
document.body.appendChild(startBtn);
startBtn.addEventListener('click', startDrawingPolygon);
var stopBtn = document.createElement('button');
stopBtn.textContent = '停止绘制多边形';
document.body.appendChild(stopBtn);
stopBtn.addEventListener('click', stopDrawingPolygon);
```
以上代码实现了在Cesium上绘制多边形并显示等值面的功能。点击“开始绘制多边形”按钮后,鼠标点击地图上的位置可以逐步绘制多边形;点击“停止绘制多边形”按钮后,将停止绘制,并在绘制多边形的基础上生成等值面。
阅读全文