cesium-2-entity

发布时间 2023-04-27 18:11:19作者: Coder-Wang

1、四层结构


viewer --> datasources(DataSourceCollection类型) --> datasource --> entities(EntityCollection类型) --> entity


需要学习的方向是:只需要注意每个层与层之间的关系和entity实例如何创建即可

2、DataSourceCollection

增:
add(dataSource) → Promise.<DataSource>
删:(destroy一般为boolean,指是否需要直接销毁该datasource)
remove(dataSource, destroy) → boolean
removeAll(destroy)
查:
indexOf(dataSource) → number
getByName(name) → Array.<DataSource>
get(index) → DataSource
contains(dataSource) → boolean
改:
该类型数据一般是指向型,直接调出属性直接修改即可
改变层级关系(特殊):
lower(dataSource)
lowerToBottom(dataSource)
raise(dataSource)
raiseToTop(dataSource)

3、datasource

这是一个抽象类,有各种实现方式
image
一般只会用到他的三个属性:entities、name和show

4、entities(EntityCollection类型)

增:
add(entity) → Entity
getOrCreateEntity(id) → Entity
删:
removeAll()
removeById(id) → boolean
remove(entity) → boolean
查:
contains(entity) → boolean
getById(id) → Entity|undefined
改:

重要属性:

id : string
owner : DataSource|CompositeEntityCollection
show : boolean
values : Array.<Entity>  // 全部的entity

5、创建新的entity

一些实例:
https://www.jianshu.com/p/4250e822c9c8

6、代码

需要注意的是这里的add()方法得到的是<promise.类型>,后面需要使用.then(成功函数,失败函数)来得到类型
add(dataSource) → Promise.<DataSource>
这种类怎么创建和怎么用见:
https://blog.csdn.net/ABCFF12333/article/details/118188018

  // 注意add()方法得到的是<promise.类型>,后面需要使用.then(成功函数,失败函数)来得到类型
  // 注意这是异步方法
  viewer.dataSources.add(new Cesium.CustomDataSource("pointDataSource1")).then(function(value){
    var pointDataSource = value;
    pointDataSource.show = true;
    var point1 = pointDataSource.entities.add({
      id: "point1",
      name: "point1",
      position: Cesium.Cartesian3.fromDegrees(109, 34, 0),
      point: {
        pixelsize: 10,
        color: Cesium.Color.YELLOW,
        outlineWidth: 2,
        outlineColor: Cesium.Color.RED
      }
    });
    var point2 = pointDataSource.entities.add({
      id: "point2",
      name: "point2",
      position: Cesium.Cartesian3.fromDegrees(110, 35, 0),
      point: {
        pixelsize: 10,
        color: Cesium.Color.YELLOW,
        outlineWidth: 2,
        outlineColor: Cesium.Color.RED
      }
    })
  },function(error){})

  viewer.dataSources.add(new Cesium.CustomDataSource("polygonDatasource")).then(function(value){
    var polygonDatasource = value;
    polygonDatasource.show = true;
    polygonDatasource.entities.add({
      id: "polygon1",
      name: "polygon1",
      polygon: {
        hierarchy: Cesium.Cartesian3.fromDegreesArray([
          109.080842, 45.002073,
          105.91517, 45.002073,
          104.058488, 44.996596,
          104.053011, 43.002989,
          104.053011, 41.003906,
          105.728954, 40.998429,
          107.919731, 41.003906,
          109.04798, 40.998429,
          111.047063, 40.998429,
          111.047063, 42.000709,
          111.047063, 44.476286,
          111.05254, 45.002073,
          109.080842, 45.002073
        ]),
        height: 10,  // 必须要有高度,否则没有边框
        material: Cesium.Color.GREEN,
        outline: true,
        outlineColor: Cesium.Color.RED,
        outlineWidth: 2,
        fill: true
      }
    })
  },function(error){})
})