news 2026/6/24 10:02:18

Cesium GeoJSON 面教程

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Cesium GeoJSON 面教程

GeoJSON 面 ·GeoJSON Polygon· ▶ 在线运行案例

  • 案例合集:三维可视化功能案例(threehub.cn)
  • 开源仓库github地址:https://github.com/z2586300277/three-cesium-examples
  • 400个案例代码:网盘链接

你将学到什么

  • GeoJsonDataSource.load加载 GeoJSON 面数据
  • 统一设置stroke / fill / strokeWidth
  • scene.pick点击 Entity 修改polygon.material
  • 封装changeMaterial批量改色

效果说明

加载广东省 GeoJSON,半透明蓝色填充;点击某一区域后该区域变为黄色半透明。

核心概念

GeoJsonDataSource

const dataSource = await Cesium.GeoJsonDataSource.load(url, {

stroke: Cesium.Color.RED.withAlpha(0.5), fill: Cesium.Color.BLUE.withAlpha(0.5), strokeWidth: 3, }); viewer.dataSources.add(dataSource); viewer.flyTo(dataSource);

加载后每个 Feature 对应一个Entity,面要素带polygon图形。

点击改色

viewer.screenSpaceEventHandler.setInputAction((event) => {

const picked = viewer.scene.pick(event.position); if (Cesium.defined(picked) && picked.id) { picked.id.polygon.material = Cesium.Color.YELLOW.withAlpha(0.5); } }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

picked.id即 Entity;polygon.material接受ColorMaterialProperty

批量换肤

dataSource.changeMaterial = (params) => {

dataSource.entities.values.forEach(entity => { entity.polygon.material = Cesium.Color.fromCssColorString(params.fillColor) .withAlpha(params.fillOpacity); entity.polygon.outlineColor = ...; }); };

实现步骤

  • 初始化 Viewer 与底图
  • setGeoPolygon(viewer, geojsonUrl)封装 load + add + changeMaterial
  • viewer.flyTo(dataSource)定位到数据范围
  • 注册 LEFT_CLICK,pick 后改 material
  • 代码要点

    import * as Cesium from 'cesium'

    const box = document.getElementById('box')

    const viewer = new Cesium.Viewer(box, {

    animation: false,//是否创建动画小器件,左下角仪表

    baseLayerPicker: false,//是否显示图层选择器,右上角图层选择按钮

    baseLayer: false, // 不显示默认图层

    fullscreenButton: false,//是否显示全屏按钮,右下角全屏选择按钮

    timeline: false,//是否显示时间轴

    infoBox: false,//是否显示信息框

    })

    const url = GLOBAL_CONFIG.getLayerUrl() const layer = Cesium.ImageryLayer.fromProviderAsync(

    Cesium.ArcGisMapServerImageryProvider.fromUrl(url)

    )

    viewer.imageryLayers.add(layer)

    // 加载geojson数据 const dataSource = setGeoPolygon(viewer, 'https://z2586300277.github.io/three-editor/dist/files/font/guangdong.json')

    // 看向geojson数据 viewer.flyTo(dataSource)

    // 点击变色 viewer.screenSpaceEventHandler.setInputAction((event) => {

    const pickedObject = viewer.scene.pick(event.position)

    if (Cesium.defined(pickedObject) && pickedObject.id) {

    pickedObject.id.polygon.material = Cesium.Color.fromCssColorString('yellow').withAlpha(0.5)

    }

    }, Cesium.ScreenSpaceEventType.LEFT_CLICK)

    // 创建 面 async function setGeoPolygon(viewer, source, params = {}) {

    const dataSource = await Cesium.GeoJsonDataSource.load(source, {

    stroke: Cesium.Color.fromCssColorString(params.strokeColor || 'red').withAlpha(params.strokeOpacity || 0.5), // 边界

    fill: Cesium.Color.fromCssColorString(params.fillColor || 'blue').withAlpha(params.fillOpacity || 0.5), // 填充

    strokeWidth: params.strokeWidth || 3,

    markerSymbol: '?',

    ...params

    })

    dataSource.changeMaterial = (params) => dataSource.entities.values.forEach(entity => {

    entity.polygon.material = Cesium.Color.fromCssColorString(params.fillColor || 'blue').withAlpha(params.fillOpacity || 0.5)

    entity.polygon.outlineColor = Cesium.Color.fromCssColorString(params.strokeColor || 'red').withAlpha(params.strokeOpacity || 0.5)

    entity.polygon.outlineWidth = params.strokeWidth || 3

    })

    viewer.dataSources.add(dataSource)

    return dataSource

    }

    完整源码:GitHub

    小结

    • 本文提供GeoJSON 面完整 Cesium.js 源码与在线 Demo,建议先运行案例再改 uniform/参数做二次实验
    • 更多 Cesium.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/24 10:01:55

ImageStrike:一站式解决18种图像隐写挑战的终极CTF安全工具

ImageStrike:一站式解决18种图像隐写挑战的终极CTF安全工具 【免费下载链接】ImageStrike ImageStrike是一款用于CTF中图片隐写的综合利用工具 项目地址: https://gitcode.com/gh_mirrors/im/ImageStrike 在网络安全竞赛和数字取证领域,图像隐写分…

作者头像 李华
网站建设 2026/6/24 10:00:26

AI伦理成熟度=企业信用新标尺:SITS 2026官方评估框架首次披露的3个隐藏维度(仅限首批参评组织内部流通)

更多请点击: https://kaifayun.com 第一章:AI伦理成熟度企业信用新标尺:SITS 2026框架的战略定位与范式跃迁 在生成式AI规模化落地的临界点上,企业不再仅以模型性能或算力密度定义竞争力,而以可验证、可审计、可追溯的…

作者头像 李华
网站建设 2026/6/24 9:50:26

为什么头部金融科技公司集体弃用某明星AI编码助手?SITS 2026深度拆解:L4级“可审计性”指标全军覆没,审计日志缺失率达91.4%

更多请点击: https://codechina.net 第一章:AI工具成熟度评测:SITS 2026开发工具链成熟度对比 当前AI开发工具链正经历从实验性原型向企业级工程化能力的关键跃迁。SITS(Software Intelligence Tooling Spectrum)2026…

作者头像 李华