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

问题描述 投票:1回答:1

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

我可以访问名称和id字段,但不能访问位置字段。位置字段包含了 "时间、经度、纬度、高度 "等制图值。我想访问这些制图值的集合,以便能够显示它们。例如,在下面的例子中,我想访问position[0]为'0.00,-4.6,-38.4,250'。我应该如何做呢?

我使用'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' };
    });
}());
javascript arrays json cesium czml
1个回答
0
投票

当你的CZML以实体形式导入时,这些原始位置已经被转换了。 在你的CZML导入实体的时候,这些原始的位置已经被转换了。entity.position 对象不是一个数组,而是一个类似于 SampledPositionProperty.

这个属性不会公开其所有的内部数据,但你可以在给定的时间请求一个位置,使用 position.getValue(...).

这里有一个 沙堡演示 显示实体位置随时间变化。

该演示的代码是这样的。

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

      }
    });
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.