目录
1 基于鼠标事件的选择,可以通过MOUSE_MOVE或LEFT_CLICK事件触发
2 由于项目要求,需要实现通过下拉列表实现选中效果,先贴代码
/*鼠标移动选择开始*/
var silhouetteBlue = Cesium.PostProcessStageLibrary.createEdgeDetectionStage();
silhouetteBlue.uniforms.color = Cesium.Color.BLUE;
silhouetteBlue.uniforms.length = 0.01;
silhouetteBlue.selected = [];
var silhouetteGreen = Cesium.PostProcessStageLibrary.createEdgeDetectionStage();
silhouetteGreen.uniforms.color = Cesium.Color.LIME;
silhouetteGreen.uniforms.length = 0.01;
silhouetteGreen.selected = [];
viewer.scene.postProcessStages.add(Cesium.PostProcessStageLibrary.createSilhouetteStage([silhouetteBlue, silhouetteGreen]));
// 设置要在输入事件上执行的功能
viewer.screenSpaceEventHandler.setInputAction(function onMouseMove(movement) {
// 将鼠标的点击坐标转换为场景中的坐标
var pickedFeature = viewer.scene.pick(movement.endPosition);
if (Cesium.defined(pickedFeature)) {
if (pickedFeature == silhouetteGreen.selected[0]) {
silhouetteBlue.selected = [];
} else {
silhouetteBlue.selected = [pickedFeature];
}
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE); // 绑定鼠标移动事件
// 绑定鼠标监听
viewer.screenSpaceEventHandler.setInputAction(function leftClick(movement) {
// 将鼠标的点击坐标转换为场景中的坐标
var pickedFeature = viewer.scene.pick(movement.position);
if (Cesium.defined(pickedFeature)) {
silhouetteGreen.selected = [pickedFeature];
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK); // 绑定左键点击事件
//加载瓦片
var tileset0 = viewer.scene.primitives.add(new Cesium.Cesium3DTileset({
url: 'map/dx/tileset.json',
skipLevelOfDetail: true, //开启跳级加载
maximumMemoryUsage: 2000, //最大内存占用
dynamicScreenSpaceError: true
}));
//给当前瓦片添加事件监听器
tileset0.tileVisible.addEventListener(function(tile) {
while (flag) {
silhouetteGreen.selected = [tile.content.getFeature(0)];
flagS = false;
}
});