当前位置: 首页 > 工具软件 > cesium > 使用案例 >

Cesium学习——Cesium实现选中效果

公羊俊
2023-12-01

本文转载自:https://blog.csdn.net/u014646677/article/details/90673113?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~first_rank_v2~rank_v25-2-90673113.nonecase&utm_term=cesium%20%E8%87%AA%E5%8A%A8%E9%80%89%E4%B8%AD%E7%89%A9%E4%BD%93

目录

1 基于鼠标事件的选择,可以通过MOUSE_MOVE或LEFT_CLICK事件触发

2 由于项目要求,需要实现通过下拉列表实现选中效果,先贴代码


1 基于鼠标事件的选择,可以通过MOUSE_MOVE或LEFT_CLICK事件触发

/*鼠标移动选择开始*/
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); // 绑定左键点击事件

2 由于项目要求,需要实现通过下拉列表实现选中效果,先贴代码

//加载瓦片
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;
    }
});

 

 类似资料: