2017-03-09 110 views
1

我希望在不同的位置產生一定數量的shape1shape2副本,這些副本只能在運行時才知道,並且能夠編程修改其他屬性。克隆和修改CZML包

提及,克隆和修改CZML包的首選方法是什麼?在加載時CzmlDataSource

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); 

回答

1

銫變爲CZML成EntityCollectionEntities充分。

但是在我進一步解釋之前,對這個dataSource做了一些說明。如果您滾動到您發佈的示例的底部,則會看到這兩行。他們來自官方示例代碼,但不幸的是他們誤導了一些人:

var dataSource = Cesium.CzmlDataSource.load(czml); 
viewer.dataSources.add(dataSource); 

變量名是一個用詞不當。 load是異步的,並將Promise返回給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中,該對象可以多次保存和重複使用。您還可以實時編輯實體上的大多數屬性以反映運行時的更改。編輯實體屬性當然比銷燬和重新創建實體要高效得多。

CZML數據包在構建EntityCollection後被丟棄,但實體ID值仍然存在。您可以使用dataSource.entities.getById('...')來查找從特定CZML數據包構建的實體。

+0

謝謝;澄清了很多。 「你不能直接克隆一個實體,但你可以從一個通用的選項對象中添加新的實體({options ...})到一個EntityCollection中,這個對象可以多次保存和重複使用。 「 如何提取從CZML數據包創建的「實體」對象的屬性(要保存到通用選項對象以供重用)? 請忍受我的沉悶;我來自C和C++的領域,並不熟悉JS的習語。 – Slaiyer

+1

@Slaiyer這不是一個典型的代碼路徑,並且Cesium不直接支持它。一種選擇可能是完全跳過CZML,而是讓服務器提供實體創建模板對象。另一個選擇是使用[CzmlDataSource.process](http://cesiumjs.org/Cesium/Build/Documentation/CzmlDataSource.html#process)以循環方式重新提取ID已更改的czml數據包。 – emackey