【超图+CESIUM】【基础API使用示例】33、超图|CESIUM - 绘制特效线段(发光|指定颜色|带边框|指定颜色箭头|虚线|动态线)

时间:2025-02-14 08:08:10
let viewer window.onload = function () { viewer = new Cesium.Viewer('cesium-container', { sceneModePicker: true, navigation: false, }) initBindDrawEventHandler() } // 初始化绑定按钮的绘制事件 function initBindDrawEventHandler() { const btns = document.querySelectorAll('.panel .btn') btns[0].addEventListener('click', () => { drawGlowingLineHandler() activeCurrentClickBtnHandler(0) }) btns[1].addEventListener('click', () => { drawSpecifyColorLineHandler() activeCurrentClickBtnHandler(1) }) btns[2].addEventListener('click', () => { drawSpecifyColorAndOutlineColorLineHandler() activeCurrentClickBtnHandler(2) }) btns[3].addEventListener('click', () => { drawSpecifyColorArrowStaticStateLineHandler() activeCurrentClickBtnHandler(3) }) btns[4].addEventListener('click', () => { drawDashedLineHandler() activeCurrentClickBtnHandler(4) }) btns[5].addEventListener('click', () => { drawDynamicLineHandler() activeCurrentClickBtnHandler(5) }) btns[6].addEventListener('click', () => { clearAllEntitiesHandler() activeCurrentClickBtnHandler(6) }) } // 1、绘制发光的线 function drawGlowingLineHandler() { viewer.entities.add({ name: 'Glowing blue line on the surface', polyline: { positions: Cesium.Cartesian3.fromDegreesArray([ 50, // 第一个点经度lon 30, // 第一个点纬度lat 130, // 第二个点经度lon 30, // 第二个点纬度lat ]), width: 5, followSurface: true, material: new Cesium.PolylineGlowMaterialProperty({ glowPower: 0.2, color: Cesium.Color.BLUE, }), }, }) } // 2、绘制指定颜色的线 function drawSpecifyColorLineHandler() { viewer.entities.add({ name: 'Red line on the surface', polyline: { positions: Cesium.Cartesian3.fromDegreesArray([50, 20, 140, 20]), width: 5, material: Cesium.Color.RED, }, }) } // 3、绘制指定颜色指定边框颜色的线 function drawSpecifyColorAndOutlineColorLineHandler() { viewer.entities.add({ name: 'Orange line with black outline at height and following the surface', polyline: { positions: Cesium.Cartesian3.fromDegreesArrayHeights([ 50, 10, 500000, 140, 10, 250000, ]), width: 10, // 线宽 material: new Cesium.PolylineOutlineMaterialProperty({ color: Cesium.Color.ORANGE, // 指定颜色 outlineWidth: 2, // 边框的宽度 outlineColor: Cesium.Color.RED, // 指定边框颜色 }), }, }) } // 4、绘制指定颜色的箭头静态指示线 function drawSpecifyColorArrowStaticStateLineHandler() { viewer.entities.add({ name: 'Purple straight arrow at height', polyline: { positions: Cesium.Cartesian3.fromDegreesArrayHeights([ 50, 0, 500000, 140, 0, 500000, ]), width: 10, followSurface: false, material: new Cesium.PolylineArrowMaterialProperty( Cesium.Color.YELLOW ), }, }) } // 5、绘制虚线 function drawDashedLineHandler(params) { viewer.entities.add({ name: 'CYAN dashed line', polyline: { positions: Cesium.Cartesian3.fromDegreesArrayHeights([ 50, -10, 500000, 140, -10, 500000, ]), width: 4, material: new Cesium.PolylineDashMaterialProperty({ color: Cesium.Color.CYAN, }), }, }) } // 6、绘制动态的线 function drawDynamicLineHandler(params) { viewer.entities.add({ name: 'RED dynamic line', polyline: { positions: Cesium.Cartesian3.fromDegreesArray([0, 45, 125, 45]), width: 10, hMax: 500000, material: new Cesium.PolylineDynamicMaterialProperty({ color: Cesium.Color.RED, outlineWidth: 0, outlineColor: Cesium.Color.BLACK, }), }, }) } // 清除所有实体 function clearAllEntitiesHandler() { viewer.entities.removeAll() } // 高亮当前点击的线段 function activeCurrentClickBtnHandler(idx) { const btns = document.querySelectorAll('.panel .btn') Array.from(btns).forEach((btn, index) => { btn.style.color = index === idx ? 'red' : '#000' }) }