【Cesium】--viewer,entity,dataSource

viewer,entity,dataSource的关系

  • viewer.entities.add({})
    通过viwer.entities.add()直接加载的entity,默认是在viewer.entities(即viewer.dataSourceDisplay.defaultDataSource.entities)中,不会加载在viewer.dataSource中。
typescript 复制代码
window.viewer.entities.add({
		position: Cesium.Cartesian3.fromDegrees(start.longitude, start.latitude, start.height),
		billboard: {
			image: imgUrl_start,
				scale: 1,
				pixelOffset: new Cesium.Cartesian2(-10, -10),
				},
			})
  • viewer.dataSources.add(dataSource)
    dataSource可以来源于直接加载json数据,也可以自己通过new Cesium.CustomDataSource()构造,可以统一控制该数据源或者entity,dataSource.entities.values就是加载json后的entities,
typescript 复制代码
	    // 也可以是一个url
		const baoHuQuJson = require("../xxx.geojson")
		Cesium.GeoJsonDataSource.load(baoHuQuJson).then(dataSource => {
				window.viewer.dataSources.add(dataSource)
				this.boudingEntities = dataSource.entities.values

				this.$bus.$on("toggleBhqBounding", () => {
					this.boudingEntities.forEach(entity => {
						// console.log(entity)
						if (entity.polyline) {
							entity.show = !entity.show
						}
					})
				})
			})

自定义dataSource,设置name,统一管理实体如点,多边形等

js 复制代码
// 根据实体创建dataSource
function createEntityDataSource(dataSourceName,entities,cb){
	const dataSource = new window.Cesium.CustomDataSource(dataSourceName)
	for(let i=0;i<entities.length;i++){
		dataSource.entities.add(entities)
	}
	
	if(typeof cb =='function'){
		cb(dataSource)
	}

	window.viewer.dataSources.add(dataSource)
	return dataSource
}

// 根据name获取dataSource
function getDatasourceByName(name,cb){
	const dataSource = window.viewer.dataSources.getByName(name)
	
	if(typeof cb == 'undefined'){
		dataSource.forEach(item=>cb(item))
	}
	
	return dataSource
}

// 切换数据源显隐
function toggoleDataSourceShow(dataSourceOrName,show){
	let target = []
	if(typeof dataSourceOrName =='string'){
		target = window.viewer.dataSources.getByName(dataSourceOrName)
	}else if(!Array.isArray(dataSourceOrName)){
		target = [dataSourceOrName]
	}
	for(const datasource of target){
		if(typeof show == 'undefined'){
			datasource.show = !datasource.show
		}else{
			datasource.show = show
		}	
	}
}

// 删除dataSource
function removeDataSource(dataSourceOrName){
	let dataSrouce = []
	if(typeof dataSourceOrName == 'string'){
		dataSrouce = window.viewer.dataSources.getByName(dataSourceOrName)
	}else if(!Array.isArray(dataSourceOrNames)){
		dataSrouce = [dataSourceOrName]
	}
	dataSrouce.forEach(item=>window.viewer.dataSources.remove(item,true))
}

dataSource添加实体

各种实体,billboard,point,polygon

js 复制代码
dataSource.entities.add({})

实体类型

  1. point
js 复制代码
const entity = {
		  id: code,
          name: pointName,
          position: Cesium.Cartesian3.fromDegrees(Number(longitude), Number(latitude), 30),// 位置,参数是 经度,维度,高度
          billboard: {// 点位显示的图标
            show: true,
            image: require(`@/assets/svg/${code}.svg`),
            scale: 1,
            rotation: 0,
          },
          properties: {// 携带的属性
            code,
            longitude,
            latitude,
          },
}
相关推荐
冬夜Zis18 分钟前
顺着作用域链和 outer 指针,一次性看懂闭包底层逻辑
javascript
Ceelog26 分钟前
Mac 上怎么找出并安全清理旧的 node_modules?(实测 304 个占 66.3 GB)
javascript
锋行天下27 分钟前
langgraph全栈项目(一)
前端·人工智能·后端
叼烟扛炮1 小时前
[LangChain] 学习笔记(15):为什么你的重试没用——429、抖动、fallback 与熔断
java·服务器·前端
小码哥0681 小时前
智慧物业系统源码定制:公告发布范围与住户阅读记录设计
前端·javascript·vue.js·物业管理·物业系统·物业软件
Highcharts.js2 小时前
Highcharts直方图使用指南:如何根据派生数据创建直方图?
javascript·信息可视化·直方图·highcharts·图表开发·创建图表·派生数据
ZhengEnCi4 小时前
Express 企业级项目结构怎么组织:五层骨架、单向依赖和一份能跑的边界检查脚本
javascript·node.js·express
泯泷4 小时前
Cloudflare 出手抢 Astro:前端框架之战,底层逻辑变了
前端·javascript·前端框架
码云社区5 小时前
Java在线租房系统源码:VR看房素材、房源图片与内容维护方案
java·vue.js·vr·租房小程序·在线租房·看房租房
cc.ChenLy5 小时前
无法修改 el-dialog 样式原因及解决办法
javascript·vue.js·elementui