cesium-切分地图

在cesium中只看想看到的部分地图,其他的隐藏。

做法如下

复制代码
import {ArrowRight} from '@element-plus/icons-vue'
import {onMounted, ref, watch} from "vue";
import * as Cesium from "cesium";
import InitCesium from "../js/InitCesiumHide.js";

初始化

复制代码
let viewer = null;
let points = ref({
	position: null
});
let checked3 = ref(true);
let checked4 = ref(true);
// 添加矩形以显示边界
const rectangles = [];
// 热带和南回归线 需要展示的区域
let coffeeBeltRectangle = Cesium.Rectangle.fromDegrees(-180.0, -23.43687, 180.0, 23.43687);

onMounted(() => {
	let initCesium = new InitCesium('cesiumContainer');
	viewer = initCesium.initViewer({});

	viewer.scene.globe.cartographicLimitRectangle = coffeeBeltRectangle;
	viewer.scene.skyAtmosphere.show = true;

	for (let i = 0; i < 10; i++) {
		rectangles.push(viewer.entities.add({
			rectangle: {
				coordinates: coffeeBeltRectangle,
				material: Cesium.Color.WHITE.withAlpha(0.0),
				height: i * 5000.0,
				outline: true,
				outlineWidth: 4.0,
				outlineColor: Cesium.Color.WHITE
			}
		}));
	}
})

监听两个值的变化,一个是是否要隐藏一个是是否显示边框

复制代码
watch([checked3, checked4], (newVal, oldVal) => {
	if (newVal[0]) {
		viewer.scene.globe.cartographicLimitRectangle = coffeeBeltRectangle;
	} else {
		viewer.scene.globe.cartographicLimitRectangle = Cesium.Rectangle.MAX_VALUE;
	}
	const rectanglesLength = rectangles.length;
	for (let i = 0; i < rectanglesLength; i++) {
		const rectangleEntity = rectangles[i];
		rectangleEntity.show = newVal[1];
	}
})

所有代码

复制代码
<template>
	<div id="cesiumContainer" style="height: 100vh;"></div>
	<div id="toolbar" style="position: fixed;top:20px;left:220px;">
		<el-breadcrumb :separator-icon="ArrowRight">
			<el-breadcrumb-item>场景</el-breadcrumb-item>
			<el-breadcrumb-item>切分地图</el-breadcrumb-item>
		</el-breadcrumb>
		<el-checkbox v-model="checked3" label="限制启用"/>
		<el-checkbox v-model="checked4" label="显示边界"/>
	</div>
</template>
<script setup>
import {ArrowRight} from '@element-plus/icons-vue'
import {onMounted, ref, watch} from "vue";
import * as Cesium from "cesium";
import InitCesium from "../js/InitCesiumHide.js";

let viewer = null;
let points = ref({
	position: null
});
let checked3 = ref(true);
let checked4 = ref(true);
// 添加矩形以显示边界
const rectangles = [];
// 热带和南回归线
let coffeeBeltRectangle = Cesium.Rectangle.fromDegrees(-180.0, -23.43687, 180.0, 23.43687);

onMounted(() => {
	let initCesium = new InitCesium('cesiumContainer');
	viewer = initCesium.initViewer({});
	// flyToRight2();
	viewer.scene.globe.cartographicLimitRectangle = coffeeBeltRectangle;
	viewer.scene.skyAtmosphere.show = true;

	for (let i = 0; i < 10; i++) {
		rectangles.push(viewer.entities.add({
			rectangle: {
				coordinates: coffeeBeltRectangle,
				material: Cesium.Color.WHITE.withAlpha(0.0),
				height: i * 5000.0,
				outline: true,
				outlineWidth: 4.0,
				outlineColor: Cesium.Color.WHITE
			}
		}));
	}
})

watch([checked3, checked4], (newVal, oldVal) => {
	if (newVal[0]) {
		viewer.scene.globe.cartographicLimitRectangle = coffeeBeltRectangle;
	} else {
		viewer.scene.globe.cartographicLimitRectangle = Cesium.Rectangle.MAX_VALUE;
	}
	const rectanglesLength = rectangles.length;
	for (let i = 0; i < rectanglesLength; i++) {
		const rectangleEntity = rectangles[i];
		rectangleEntity.show = newVal[1];
	}
})
</script>
<style scoped>
#cesiumContainer {
	overflow: hidden;
}
</style>
<style>
.el-breadcrumb__inner, .el-breadcrumb__separator {
	color: #ffffff !important;
}
</style>

效果图如下

相关推荐
一个不爱写代码的瘦子30 分钟前
迭代器和生成器
前端·javascript
拳打南山敬老院31 分钟前
漫谈 MCP 构建之概念篇
前端·后端·aigc
前端老鹰32 分钟前
HTML <output> 标签:原生表单结果展示容器,自动关联输入值
前端·html
OpenTiny社区33 分钟前
OpenTiny NEXT 内核新生:生成式UI × MCP,重塑前端交互新范式!
前端·开源·agent
耶耶耶11139 分钟前
web服务代理用它,还不够吗?
前端
Liamhuo1 小时前
2.1.7 network-浏览器-前端浏览器数据存储
前端·浏览器
洋葱头_1 小时前
vue3项目不支持低版本的android,如何做兼容
前端·vue.js
前端小书生1 小时前
React 组件渲染
前端·react.js
sjd_积跬步至千里1 小时前
CSS实现文字横向无限滚动效果
前端
维他AD钙2 小时前
前端基础避坑:3 个实用知识点的简单用法
前端