cesium.js 绘制城市边界

/**
 * 绘制反选遮罩 —— 添加城市边界
 * '../../../json/addBeijingBoundary.json'
 * 参考:
 * https://blog.csdn.net/weixin_38676065/article/details/123977289
 * https://blog.csdn.net/qq_38370387/article/details/124730066
 *
 * 需要 viewer 挂载到 window 上(或者自行修改代码)
 * Cesium 默认在index.html引入,如果使用的 npm 自行修改代码
 */
import axios from 'axios'

const cityBoundaryJSON = 'http://192.168.0.115:8091';

function addCityBoundary(path) {
    axios.request({
        url: path,
        method: 'get',
        baseURL: cityBoundaryJSON
    }).then(function (response) {
        let feature = response.data;
        let coordinates = feature.geometry.coordinates[0];

        let degreesArrayHeights = [];
        for (let i = 0; i < coordinates.length; i++) {
            const element = coordinates[i];
            degreesArrayHeights.push(element[0]);
            degreesArrayHeights.push(element[1]);
            degreesArrayHeights.push(0);
        }

        let positions = Cesium.Cartesian3.fromDegreesArrayHeights(degreesArrayHeights);
        // 添加遮罩层
        window.viewer.entities.add({
            name: "convertLayer",
            polygon: {
                hierarchy: {
                    positions: Cesium.Cartesian3.fromDegreesArray([
                        100, 0, 100, 89, 150, 89, 150, 0,
                    ]),
                    holes: [{positions}],
                },
                material: Cesium.Color.fromCssColorString('#061824').withAlpha(0.6)
            },
            heightReference: Cesium.HeightReference.CLAMP_TO_GROUND // 表示相对于地形的位置
        })
        // 添加边线
        window.viewer.entities.add({
            id: 2,
            polyline: {
                positions: positions,
                width: 2,
                material: Cesium.Color.fromCssColorString('#6dcdeb')
            }
        })
    }).catch(function (error) {
        console.log(error);
    });
}

export {addCityBoundary}

json文件是geojson格式的

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值