【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,
          },
}
相关推荐
2501_920931702 小时前
React Native鸿蒙跨平台采用ScrollView的horizontal属性实现横向滚动实现特色游戏轮播和分类导航
javascript·react native·react.js·游戏·ecmascript·harmonyos
0思必得04 小时前
[Web自动化] Selenium处理动态网页
前端·爬虫·python·selenium·自动化
东东5164 小时前
智能社区管理系统的设计与实现ssm+vue
前端·javascript·vue.js·毕业设计·毕设
catino4 小时前
图片、文件的预览
前端·javascript
2501_920931706 小时前
React Native鸿蒙跨平台实现推箱子游戏,完成玩家移动与箱子推动,当所有箱子都被推到目标位置时,玩家获胜
javascript·react native·react.js·游戏·ecmascript·harmonyos
layman05286 小时前
webpack5 css-loader:从基础到原理
前端·css·webpack
半桔6 小时前
【前端小站】CSS 样式美学:从基础语法到界面精筑的实战宝典
前端·css·html
AI老李6 小时前
PostCSS完全指南:功能/配置/插件/SourceMap/AST/插件开发/自定义语法
前端·javascript·postcss
_OP_CHEN6 小时前
【前端开发之CSS】(一)初识 CSS:网页化妆术的终极指南,新手也能轻松拿捏页面美化!
前端·css·html·网页开发·样式表·界面美化
啊哈一半醒6 小时前
CSS 主流布局
前端·css·css布局·标准流 浮动 定位·flex grid 响应式布局