Cesium通过viewer. entities来控制entity的加载、删除 。首先加载一个entity,如下
var position = Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706, 1212);
var url = '../../SampleData/models/CesiumAir/Cesium_Air.glb';
var heading = Cesium.Math.toRadians(135);
var pitch = 0;
var roll = 0;
var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
var orientation = Cesium.Transforms.headingPitchRollQuaternion(position, hpr);
var entity = viewer.entities.add({
name : url,
position : position,
orientation : orientation,
model : {
uri : url,
minimumPixelSize : 128,
maximumScale : 20000
}
});
下面是对entity的所有操作:
viewer.trackedEntity = entity;//目标视野跟踪
viewer. entities.remove(entity)//删除entity
viewer. entities.removeById(id)//根据id删除entity
viewer. entities.removeAll()//清空所有entity
viewer. entities.getById(id)// 获取enetity
viewer. entities.contains(entity)// 是否包含
在绘制entity时,指定parent属性为某个集合,然后通过show来控制同属一个parent的所有entity的显隐。
var near_parent_entity = viewer.entities.add(new Cesium.Entity());
var tarentity = viewer.entities.add({
id:item.id,
//Use our computed positions
parent:near_ parent_entity,
position : position,
color : Cesium.Color.fromAlpha(Cesium.Color.BLUE,
parseFloat(0.5)),
//Load the Cesium plane model to represent the entity
model : {
uri : '/static/Cesium/models/aa.gltf',
minimumPixelSize : 1,
scale : 0.09
}
});
near_parent_entity.show = true;
tarentity.model.color = Cesium.Color.YELLOW;//修改模型颜色