cesium代码实现绘制矩形
时间: 2023-09-06 14:10:07 浏览: 165
好的,以下是用Cesium实现绘制矩形的代码:
```javascript
var viewer = new Cesium.Viewer('cesiumContainer');
var handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
var rect = null;
var startPoint = null;
var endPoint = null;
handler.setInputAction(function(click) {
startPoint = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
handler.setInputAction(function(movement) {
endPoint = viewer.camera.pickEllipsoid(movement.endPosition, viewer.scene.globe.ellipsoid);
if (!Cesium.defined(rect)) {
rect = new Cesium.Rectangle();
}
rect.north = Math.max(startPoint.latitude, endPoint.latitude);
rect.south = Math.min(startPoint.latitude, endPoint.latitude);
rect.east = Math.max(startPoint.longitude, endPoint.longitude);
rect.west = Math.min(startPoint.longitude, endPoint.longitude);
viewer.entities.add({
rectangle: {
coordinates: rect,
fill: false,
outline: true,
outlineColor: Cesium.Color.WHITE
}
});
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
```
这段代码的作用是在Cesium地图中实现鼠标绘制矩形功能。当用户在地图上左击后,代码会保存下点击位置,并监听鼠标移动事件。当鼠标开始移动时,代码会获取当前鼠标位置,并计算出矩形的四个坐标。最后,代码将创建一个矩形实体,并添加到Cesium场景中。
阅读全文