Cesium添加和取消相机跟踪+时间轴暂停和启动+根据name移除CZML数据
- 具体实现代码:
var viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider: Cesium.createWorldTerrain(),
baseLayerPicker: false,
shouldAnimate: true,
});
viewer._cesiumWidget._creditContainer.style.display = "none";//去除版权信息
var ds_data;
viewer.dataSources.add(Cesium.CzmlDataSource.load(czml)).then(function (ds) {
ds_data=ds.entities.getById("KongKeA310");// 用相机跟踪czml上的模型。
viewer.trackedEntity = ds_data;
// 取消双击事件-追踪该位置
viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
// 取消左键单击事件-追踪该位置
viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
});
//开始相机跟踪
var camera = document.getElementById("Start_camera_tracking");
camera.onclick =function(){
viewer.trackedEntity = ds_data;
viewer.clock.canAnimate = true;//时间轴运行
viewer.clock.shouldAnimate = true;//时间轴运行
};
//取消相机跟踪
var btn = document.getElementById("button");
btn.onclick =function(){
viewer.trackedEntity =undefined;//取消相机跟踪 //取消相机跟踪
viewer.clock.canAnimate = false;//时间轴暂停
viewer.clock.shouldAnimate = false;//时间轴暂停
};
//移除CZML并停止时间轴运行
var remove1 = document.getElementById("removeCZML");
remove1.onclick =function(){
viewer.clock.canAnimate = false;//时间轴暂停
viewer.clock.shouldAnimate = false;//时间轴暂停
for (var a= 0; a < viewer.dataSources.length; a++) {
var str=viewer.dataSources.get(a).name;
console.log("str=="+ str);
//str.includes("")返回一个布尔值,值为true时表示包含
if(str.includes('KongKeA310')){
viewer.dataSources.remove(viewer.dataSources.get(a));
}
}
};
- 运行截图: