从文件加载czml/json数据并将其存储到变量中

问题描述:

我使用的是由Cesium提供的Sandcastle接口,但是我的问题可以扩展(我猜)可以用于任何javascript用户。我的目标是加载一个czml文件(也可以是一个.json文件),并将其内容存储到一个变量中,以便简单地访问它的标签。从文件加载czml/json数据并将其存储到变量中

在这里有一个小例子:让我们假设该文件(test.czml)内容如下:

[{ 
    "id" : "document", 
    "name" : "Basic CZML billboard and label", 
    "version" : "1.0" 
}, { 
    "id" : "Point A", 
    "name" : "Point A", 
    "label" : { 
     "fillColor" : { 
      "rgba" : [255, 255, 255, 255] 
     }, 
     "font" : "12pt Lucida Console", 
     "horizontalOrigin" : "LEFT", 
     "pixelOffset" : { 
      "cartesian2" : [8, 0] 
     }, 
     "style" : "FILL", 
     "text" : "Point A", 
     "showBackground" : true, 
     "backgroundColor" : { 
      "rgba" : [112, 89, 57, 200] 
     } 
    }, 
    "position" : { 
     "cartographicDegrees":[ 
      0, 0, 0 
     ] 
    } 
}, { 
    "id" : "Point B", 
    "name" : "Point B", 
    "label" : { 
     "fillColor" : { 
      "rgba" : [255, 255, 255, 255] 
     }, 
     "font" : "12pt Lucida Console", 
     "horizontalOrigin" : "LEFT", 
     "pixelOffset" : { 
      "cartesian2" : [8, 0] 
     }, 
     "style" : "FILL", 
     "text" : "Point B", 
     "showBackground" : true, 
     "backgroundColor" : { 
      "rgba" : [112, 89, 57, 200] 
     } 
    }, 
    "position" : { 
     "cartographicDegrees":[ 
      0, 10, 0 
     ] 
    } 
}] 

我的最终目标是访问下列方式内容:

var czml = function_to_load_data(test.czml) 
console.log(czml[1].id) 

我是能够通过使用下面的命令来加载数据:

var czml = Cesium.CzmlDataSource.load('../../SampleData/simple.czml')); 

但这种方法不能按我的要求工作。什么是实现我目标的巧妙和优雅的方式?

可能的方式,我能够实现我的解决方案中的铯:

var viewer = new Cesium.Viewer('cesiumContainer'); 

Cesium.loadJson('../../SampleData/test.czml').then(function(jsonData) { 

    console.log(jsonData[1].id); 

}).otherwise(function(error) { 
    // an error occurred 
});