【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,
          },
}
相关推荐
蜡笔小新星27 分钟前
Flask项目框架
开发语言·前端·经验分享·后端·python·学习·flask
计算机学姐30 分钟前
基于Asp.net的驾校管理系统
vue.js·后端·mysql·sqlserver·c#·asp.net·.netcore
Fantasywt4 小时前
THREEJS 片元着色器实现更自然的呼吸灯效果
前端·javascript·着色器
IT、木易5 小时前
大白话JavaScript实现一个函数,将字符串中的每个单词首字母大写。
开发语言·前端·javascript·ecmascript
Mr.NickJJ5 小时前
JavaScript系列06-深入理解 JavaScript 事件系统:从原生事件到 React 合成事件
开发语言·javascript·react.js
ZXT6 小时前
面试精讲 - vue3组件之间的通信
vue.js
张拭心7 小时前
2024 总结,我的停滞与觉醒
android·前端
念九_ysl7 小时前
深入解析Vue3单文件组件:原理、场景与实战
前端·javascript·vue.js
Jenna的海糖7 小时前
vue3如何配置环境和打包
前端·javascript·vue.js
Mr.NickJJ7 小时前
React Native v0.78 更新
javascript·react native·react.js