【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,
          },
}
相关推荐
UIUV2 分钟前
JavaScript中this指向机制与异步回调解决方案详解
前端·javascript·代码规范
momo1002 分钟前
IndexedDB 实战:封装一个通用工具类,搞定所有本地存储需求
前端·javascript
liuniansilence2 分钟前
🚀 高并发场景下的救星:BullMQ如何实现智能流量削峰填谷
前端·分布式·消息队列
再花2 分钟前
在Angular中实现基于nz-calendar的日历甘特图
前端·angular.js
San308 分钟前
从零到一:彻底搞定面试高频算法——“列表转树”与“爬楼梯”全解析
javascript·算法·面试
GISer_Jing14 分钟前
今天看了京东零售JDS的保温直播,秋招,好像真的结束了,接下来就是论文+工作了!!!加油干论文,学&分享技术
前端·零售
Mapmost21 分钟前
【高斯泼溅】如何将“歪头”的3DGS模型精准“钉”在地图上,杜绝后续误差?
前端
JellyDDD23 分钟前
h5上传大文件可能会导致手机浏览器卡死,重新刷新的问题
javascript·上传文件
废春啊1 小时前
前端工程化
运维·服务器·前端
爱上妖精的尾巴1 小时前
6-9 WPS JS宏Map、 set、get、delete、clear()映射的添加、修改、删除
前端·wps·js宏·jsa