我希望在不同的位置生成一定数量的shape1和shape2副本,这些副本只能在运行时才知道,并且能够以编程方式更改它们的其他属性。
引用、克隆和修改CZML数据包的首选方法是什么?
var czml = [{
"id" : "document",
"name" : "CZML Geometries: Cones and Cylinders",
"version" : "1.0"
}, {
"id" : "shape1",
"name" : "Green cylinder with black outline",
"position" : {
"cartographicDegrees" : [-100.0, 40.0, 200000.0]
},
"cylinder" : {
"length" : 400000.0,
"topRadius" : 200000.0,
"bottomRadius" : 200000.0,
"material" : {
"solidColor" : {
"color" : {
"rgba" : [0, 255, 0, 128]
}
}
},
"outline" : true,
"outlineColor" : {
"rgba" : [0, 0, 0, 255]
}
}
}, {
"id" : "shape2",
"name" : "Red cone",
"position" : {
"cartographicDegrees" : [-105.0, 40.0, 200000.0]
},
"cylinder" : {
"length" : 400000.0,
"topRadius" : 0.0,
"bottomRadius" : 200000.0,
"material" : {
"solidColor" : {
"color" : {
"rgba" : [255, 0, 0, 255]
}
}
}
}
}];
var dataSource = Cesium.CzmlDataSource.load(czml);
viewer.dataSources.add(dataSource);发布于 2017-03-10 16:15:10
当加载CZML时,铯会将CZML转换为充满EntityCollection的实体。
但在我进一步解释之前,我要先澄清一下这个dataSource。如果你滚动到你发布的例子的底部,你会看到这两行。它们来自官方的示例代码,但不幸的是,它们误导了一些人:
var dataSource = Cesium.CzmlDataSource.load(czml);
viewer.dataSources.add(dataSource);变量名是用词不当。load是异步的,并将Promise返回给dataSource,而不是实际的dataSource。要获得对实际dataSource的引用,必须在承诺解决时得到一个回调:
Cesium.CzmlDataSource.load(czml).then(function(dataSource) {
viewer.dataSources.add(dataSource);
// do more things with dataSource...
});现在您有了一个真正的dataSource (在异步回调中),您可以找到像dataSource.entities这样的属性,这就是您的EntityCollection。
您不能直接克隆一个实体,但是可以从一个可以多次保存和重用的通用选项对象中将new Entity({ options... })添加到EntityCollection中。您还可以实时编辑实体上的大多数属性,以反映运行时的更改。当然,编辑实体属性比破坏和重新创建实体要好得多。
在构建EntityCollection之后,CZML数据包将被丢弃,但实体ID值将保持不变。您可以使用DataSource.entities.getById(‘.’)查找从特定CZML数据包构建的实体。
https://stackoverflow.com/questions/42707232
复制相似问题