Cesium各类实体的添加

该文章已生成可运行项目,

(一)Entity API简介

1.立方体:

var blueBox = viewer.entities.add({
    name : 'Blue box',
     //中心的位置
    position: Cesium.Cartesian3.fromDegrees(-114.0, 40.0, 300000.0),
    box : {
        //长宽高
        dimensions : new Cesium.Cartesian3(400000.0, 300000.0, 500000.0),
        material : Cesium.Color.BLUE
    }
});
 
var redBox = viewer.entities.add({
    name : 'Red box with black outline',
    position: Cesium.Cartesian3.fromDegrees(-107.0, 40.0, 300000.0),
    box : {
        dimensions : new Cesium.Cartesian3(400000.0, 300000.0, 500000.0),
        material : Cesium.Color.RED,
        outline : true, //显示轮廓
        outlineColor : Cesium.Color.BLACK
    }
});
 
var outlineOnly = viewer.entities.add({
    name : 'Yellow box outline',
    position: Cesium.Cartesian3.fromDegrees(-100.0, 40.0, 300000.0),
    box : {
        dimensions : new Cesium.Cartesian3(400000.0, 300000.0, 500000.0),
        fill : false,  //不显示填充
        outline : true,
        outlineColor : Cesium.Color.YELLOW
    }
});

2.圆和椭圆

var viewer = new Cesium.Viewer('cesiumContainer');
//浮空的绿色圆形
var greenCircle = viewer.entities.add({
    position: Cesium.Cartesian3.fromDegrees(-111.0, 40.0, 150000.0),
    name : 'Green circle at height',
    ellipse : {
        semiMinorAxis : 300000.0,
        semiMajorAxis : 300000.0,
        height: 200000.0, //浮空
        material : Cesium.Color.GREEN
    }
});
//红色椭圆形,位于地表,带轮廓
var redEllipse = viewer.entities.add({
    //不带高度
    position: Cesium.Cartesian3.fromDegrees(-103.0, 40.0),
    name : 'Red ellipse on surface with outline',
    ellipse : {
        semiMinorAxis : 250000.0,
        semiMajorAxis : 400000.0,
        material : Cesium.Color.RED.withAlpha(0.5),
        outline : true,
        outlineColor : Cesium.Color.RED
    }
});
//蓝色椭圆柱,旋转了角度
var blueEllipse = viewer.entities.add({
    position: Cesium.Cartesian3.fromDegrees(-95.0, 40.0, 100000.0),
    name : 'Blue translucent, rotated, and extruded ellipse',
    ellipse : {
        semiMinorAxis : 150000.0,
        semiMajorAxis : 300000.0,
        extrudedHeight : 200000.0,  //拉伸
        rotation : Cesium.Math.toRadians(45), //旋转
        material : Cesium.Color.BLUE.withAlpha(0.7),
        outline : true
    }
});
 
viewer.zoomTo(viewer.entities);

3.圆柱和圆锥(Cylinder Cones)

var greenCylinder = viewer.entities.add({
    name : 'Green cylinder with black outline',
    position: Cesium.Cartesian3.fromDegrees(-100.0, 40.0, 200000.0),
    cylinder : { //圆柱
        length : 400000.0,
        topRadius : 200000.0,
        bottomRadius : 200000.0,
        material : Cesium.Color.GREEN,
        outline : true,
        outlineColor : Cesium.Color.DARK_GREEN
    }
});
 
var redCone = viewer.entities.add({
    name : 'Red cone',
    position: Cesium.Cartesian3.fromDegrees(-105.0, 40.0, 200000.0),
    cylinder : {//圆锥
        length : 400000.0,
        topRadius : 0.0,
        bottomRadius : 200000.0,
        material : Cesium.Color.RED
    }
});
4.墙
var redWall = viewer.entities.add({
    name : 'Red wall at height',
    wall : {
        positions : Cesium.Cartesian3.fromDegreesArrayHeights(
             [-115.0, 44.0, 200000.0,//坐标点
              -90.0, 44.0, 200000.0]
        ),
        minimumHeights : [100000.0, 100000.0], //按坐标点的最小高度数组
        material : Cesium.Color.RED
    }
});
//四边围墙
var greenWall = viewer.entities.add({
    name : 'Green wall from surface with outline',
    wall : {
        positions : Cesium.Cartesian3.fromDegreesArrayHeights(
            [-107.0, 43.0, 100000.0,
             -97.0, 43.0, 100000.0,
             -97.0, 40.0, 100000.0,
             -107.0, 40.0, 100000.0,
             -107.0, 43.0, 100000.0]),
        material : Cesium.Color.GREEN,
        outline : true,
        outlineColor : Cesium.Color.BLACK
    }
});
5.点、图标和标签
var viewer = new Cesium.Viewer( 'cesiumContainer' );
 
var citizensBankPark = viewer.entities.add( {
    name : 'Citizens Bank Park',
    position : Cesium.Cartesian3.fromDegrees( -75.166493, 39.9060534 ),
    point : { //点
        pixelSize : 5,
        color : Cesium.Color.RED,
        outlineColor : Cesium.Color.WHITE,
        outlineWidth : 2
    },
    label : { //文字标签
        text : 'Citizens Bank Park',
        font : '14pt monospace',
        style : Cesium.LabelStyle.FILL_AND_OUTLINE,
        outlineWidth : 2,
        verticalOrigin : Cesium.VerticalOrigin.BOTTOM, //垂直方向以底部来计算标签的位置
        pixelOffset : new Cesium.Cartesian2( 0, -9 )   //偏移量
    }
    billboard : { //图标
        image : '.png',
        width : 64,
        height : 64
    },
} );
 
6.3D模型
var viewer = new Cesium.Viewer('cesiumContainer');
var position = Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706); //位置
var heading = Cesium.Math.toRadians(45.0);//绕垂直于地心的轴旋转
var pitch = Cesium.Math.toRadians(15.0);  //绕纬度线旋转
var roll = Cesium.Math.toRadians(0.0);    //绕经度线旋转
var orientation = Cesium.Transforms.headingPitchRollQuaternion(position, heading, pitch, roll);
 
var entity = viewer.entities.add({
    position : position,
    orientation : orientation,
    model : {
        uri : '支持gltf模型'
    }
});
viewer.trackedEntity = entity;
7.根据经纬度贴图片
Viewer.entities.add({
	Rectangle:{
		Coordinates:Cesium.Rectangle.fromDegrees(),
		Material: new Cesium.ImageMaterialProperty({
			Image:'./png',
		})
	}
});

(二)Primitive API

1.绘制矩形
var viewer = new Cesium.Viewer('cesiumContainer');
var scene = viewer.scene;
 
scene.primitives.add(new Cesium.RectanglePrimitive({
    //绘制矩形
    rectangle : Cesium.Rectangle.fromDegrees(-100.0, 20.0, -90.0, 30.0),
    material : Cesium.Material.fromType('Dot')  //设置材质
}));
2.绘制多线段
var primitive = new Cesium.Primitive({
  geometryInstances : new Cesium.GeometryInstance({
    geometry : new Cesium.PolylineGeometry({
      positions : Cesium.Cartesian3.fromDegreesArrayHeights([
        0.0, 0.0,1000
        5.0, 0.0,5000
      ]),
      width : 10.0,
      vertexFormat : Cesium.PolylineColorAppearance.VERTEX_FORMAT
    }),
    attributes : {
      color : Cesium.ColorGeometryInstanceAttribute.fromColor(new Cesium.Color(1.0, 1.0, 1.0, 1.0))
    }
  }),
  appearance : new Cesium.PolylineColorAppearance({
    translucent : false
  })});
viewer.scene.primitives.add(primitive);
3.绘制添加点
var points = scene.primitives.add(new Cesium.PointPrimitiveCollection());

points.add({
	color:Cesium.Color.RED,
	pixelSize:size,
	position: new Cesium.Cartesian3.fromDegrees(log,lat,height),
})
本文章已经生成可运行项目
### 如何在 Cesium 中为实体添加 Popup 功能 要在 Cesium 中实现为实体添加弹出窗口 (Popup) 的功能,通常可以通过监听鼠标事件来触发特定行为。以下是具体的方法: #### 使用 `viewer.screenSpaceEventHandler` 来捕获点击事件 Cesium 提供了一个强大的工具——屏幕空间事件处理器 (`screenSpaceEventHandler`),可以用来检测用户的交互操作。通过绑定 `LEFT_CLICK` 事件到目标实体上,可以在用户单击某个实体时显示自定义的弹窗。 ```javascript // 初始化 Cesium Viewer const viewer = new Cesium.Viewer('cesiumContainer'); // 创建一个示例实体 const entity = viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(-75.1641, 39.9525), point: { pixelSize: 10, color: Cesium.Color.RED }, name: 'Example Entity' }); viewer.zoomTo(entity); // 添加屏幕空间事件处理程序 const handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas); handler.setInputAction(function(movement) { const pickedObject = viewer.scene.pick(movement.position); // 获取拾取的对象 if (Cesium.defined(pickedObject) && pickedObject.id === entity) { // 判断是否选中指定实体 showPopup(entity.name); // 调用函数展示弹窗 } }, Cesium.ScreenSpaceEventType.LEFT_CLICK); function showPopup(name) { alert(`You clicked on the entity named: ${name}`); // 这里可以用更复杂的 UI 替代 alert } ``` 以上代码展示了如何创建一个简单的弹窗效果[^1]。当用户点击地图上的某一点时,如果该点对应于我们预先设置好的实体,则会调用 `showPopup()` 函数并传递相应的参数。 #### 集成 HTML/CSS 实现高级 Popups 对于更加复杂和美观的用户体验,建议使用原生 DOM 或第三方库(如 Bootstrap Modal)构建动态弹框界面。下面是一个基于纯 JavaScript 和 CSS 的例子: ```html <div id="popup-container" style="display:none;position:absolute;z-index:100;"> <div id="popup-content"></div> </div> <style> #popup-container { background-color:white; border-radius:8px; padding:1em; box-shadow:rgba(0,0,0,.2) 0 2px 4px; } #popup-container::after,#popup-container::before{ content:""; display:block; width:0;height:0;border-style:solid; top:-10px;left:auto;right:20%;bottom:auto;margin-left:-10px; position:absolute; } #popup-container::after{border-width:10px;border-color:#fff transparent transparent;} #popup-container::before{border-width:12px;border-color:rgba(0,0,0,.2) transparent transparent;} </style> ``` 更新之前的脚本部分如下所示: ```javascript let popupDiv = document.getElementById('popup-container'); let popupContent = document.getElementById('popup-content'); handler.setInputAction((movement)=>{ let pickResult = viewer.scene.pick(movement.position); if(Cesium.defined(pickResult)){ let ent = pickResult.id; if(ent){ updateAndShowPopup(ent,movement.endPosition.x,movement.endPosition.y); }else{ hidePopup(); } }else{ hidePopup(); } },{type:Cesium.ScreenSpaceEventType.MOUSE_MOVE}); function updateAndShowPopup(entity,xPos,yPos){ popupContent.innerHTML=`<strong>${entity.name}</strong><br/>Details about this object.`; popupDiv.style.left=xPos+'px'; popupDiv.style.top=yPos+'px'; popupDiv.style.display='block'; } function hidePopup(){ popupDiv.style.display='none'; } ``` 此段代码实现了随着鼠标的移动实时更新位置以及隐藏逻辑[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值