首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Cesiumjs从加载的CZML数据中访问位置值

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

Stack Overflow用户
提问于 2020-05-16 18:40:32
回答 1查看 570关注 0票数 1

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

我可以访问name和id字段,但不能访问position字段。位置字段包含‘时间,经度,纬度,高度’这样的地图值。我想访问这些地图值的集合,这样我就可以显示它们。例如,对于下面的例子,我想访问的位置是'0.00,-4.6,-38.4,250‘。我该怎么做呢?

我使用'Cesium.CzmlDataSource.load‘加载数据,如下所示。我还可以附加一个像'model‘这样的新字段,但不能访问position字段。

CZML文件

代码语言:javascript
复制
[{
    "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

代码语言:javascript
复制
(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' };
    });
}());
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-17 04:08:18

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

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

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

该演示的代码如下所示:

代码语言:javascript
复制
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";

      }
    });
  }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61835459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档