1

我将 CZML 文件加载到我的 app.js 文件中[下面提供了两个文件]。

我可以访问 name 和 id 字段,但不能访问 position 字段。位置字段包含制图值,如“时间、经度、纬度、高度”。我想访问这些制图值的集合,以便显示它们。例如,对于下面的示例,我想将 position[0] 访问为 '0.00,-4.6,-38.4,250'。我该怎么做呢?

我使用“Cesium.CzmlDataSource.load”加载数据,如下所示。我还可以附加一个像“模型”这样的新字段,但不能访问位置字段。

CZML 文件

[{
    "id":"document",
    "name":"test",
    "version":"1.0",
},
{
    "id":"field1",
    "name":"one",
    "position":
    {
        "cartographicDegrees": [
                   0.00,-4.6,-38.4,250,
                   0.00,-4.607,-38.491,249,
                   0.15,-4.6079,-38.48,249]
    }
}
]

应用程序.js

(function () {
    "use strict";
    var viewer = new Cesium.Viewer('cesiumContainer');

   var readPromise = Cesium.CzmlDataSource.load('./test.czml');

    // Save a new drone model entity
    var testobj;
    readPromise.then(function(dataSource) 
    {
        viewer.dataSources.add(dataSource);

        var ds = viewer.dataSources.get(0);
        console.log("# of ds loaded: " + ds.entities.values.length);
        console.log("ds id: " + ds.entities.values[0].id);
        console.log("ds name: " + ds.entities.values[0].name);

        // Output of following line - [object, Object] ???
        console.log("ds name: " + ds.entities.values[0].position);

        // Get the entity using the id defined in the CZML data
        drone = dataSource.entities.getById('field1');

        // Attach a 3D model
        drone.model = { uri : './Source/SampleData/Models/drone.glb' };
    });
}());
4

1 回答 1

0

当您的 CZML 作为实体导入时,这些原始位置已被转换。您正在访问的entity.position对象不是数组,而是SampledPositionProperty.

此属性不会公开其所有内部数据,但您可以使用position.getValue(...).

这是一个Sandcastle 演示,显示了实体位置随时间变化。

该演示的代码如下所示:

var viewer = new Cesium.Viewer("cesiumContainer", {
  shouldAnimate: true,
});
var toolbar = document.getElementById("toolbar");

// Pre-allocate some memory, so we don't re-allocate 30~60 times per second.
var scratchCartesian = new Cesium.Cartesian3();
var scratchCartographic = new Cesium.Cartographic();

Cesium.CzmlDataSource.load("../SampleData/Vehicle.czml").then(function(dataSource) {
  viewer.dataSources.add(dataSource);
  viewer.clock.multiplier = 1;
  var entity = dataSource.entities.getById("Vehicle");
  if (entity) {
    // Track our entity with the camera.
    viewer.trackedEntity = entity;
    viewer.clock.onTick.addEventListener(function(clock) {

      // Get the position of our entity at the current time, if possible (otherwise undefined).
      var pos = entity.position.getValue(clock.currentTime, scratchCartesian);
      if (pos) {

        // If position is valid, convert from Cartesian3 to Cartographic.
        var lla = Cesium.Cartographic.fromCartesian(pos, Cesium.Ellipsoid.WGS84,
                  scratchCartographic);

        // Finally, convert from radians to degrees.
        toolbar.innerHTML =
          "Longitude: " + Cesium.Math.toDegrees(lla.longitude).toFixed(4) + " deg\n" +
          " Latitude:   " + Cesium.Math.toDegrees(lla.latitude).toFixed(4) + " deg\n" +
          " Altitude:   " + Cesium.Math.toDegrees(lla.height).toFixed(4) + " m";

      }
    });
  }
});
于 2020-05-16T20:08:18.013 回答