使用 Cesiumjs 从加载的 CZML 数据访问位置值

Access position values from loaded CZML data using Cesiumjs

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

我可以访问名称和 ID 字段,但不能访问职位字段。位置字段包含制图值 'time, longitude, latitude, altitude'。我想访问这些制图值的集合,以便显示它们。例如,对于下面的示例,我想以“0.00,-4.6,-38.4,250”的形式访问 position[0]。我该怎么做呢?

我使用 'Cesium.CzmlDataSource.load' 加载数据,如下所示。我还可以附加一个新字段,例如 'model',但不能访问位置字段。

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]
    }
}
]

app.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' };
    });
}());

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

的一个实例

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

在给定时间请求职位

这里是 Sandcastle Demo 显示实体位置随时间变化的。

该演示的代码如下所示:

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

      }
    });
  }
});