cesium 使用 GeoJsonDataSource 加载 geoJson 数据,渲染 Polygon、polyline、Text 贴地配置

原文地址:https://juejin.cn/post/7029592051154944007

 

复制代码
export function renderPolygon(geojson: any, zoomto: Boolean = true) {
    const viewer = window.viewer
    const dataSource = new Cesium.GeoJsonDataSource()

    // [cesium加载geoJson数据] https://juejin.cn/post/7029592051154944007
    dataSource.load(geojson, { clampToGround: true }).then(() => {
        viewer.dataSources.add(dataSource)
        const entities = dataSource.entities.values
        for (let i = 0; i < entities.length; i++) {
            const entity = entities[i]
            // 修改 entity 样式
            entity.polygon.material = Cesium.Color.fromCssColorString('#ff0000').withAlpha(0.5)
            // 添加 entity 的 polyline
            entity.polyline = { positions: entity.polygon.hierarchy._value.positions, width: 2, material: Cesium.Color.fromCssColorString('#ffff00') }
            // 获取一个 entity 的中心位置
            const center = Cesium.BoundingSphere.fromPoints(entity.polygon.hierarchy._value.positions).center
            // 设置中心位置
            entity.position = center
            // 添加 text
            entity.label = {
                text: entity.properties.name,
                color: Cesium.Color.fromCssColorString('#fff'),
                font: 'normal 32px MicroSoft YaHei',
                showBackground: true,
                scale: 0.5,
                horizontalOrigin: Cesium.HorizontalOrigin.LEFT_CLICK,
                verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                disableDepthTestDistance: 10000.0
            }
        }

        if (zoomto) {
            viewer.zoomTo(dataSource)
        }
    })

    return dataSource
}
复制代码

 

posted @   贝尔塔猫  阅读(2567)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
历史上的今天:
2022-04-18 docker-compose 部署 RSS 服务订阅、安装tiny-tiny-rss、RSSHub
2018-04-18 express 设置header解决跨域问题
2017-04-18 javascript 温故而知新 getBoundingClientRect
2017-04-18 IOS与安卓的远程调试
2016-04-18 css margin居中的问题
点击右上角即可分享
微信分享提示