I load in a CZML file into my app.js file [both files are provided below].
I'm able to access the name and id fields but not the position field. The position field contains cartographic values as 'time, longitude, latitude, altitude'. I'd like to access sets of these cartographic values so I can display them. For example, for the example below, I'd like to access position[0] as '0.00,-4.6,-38.4,250'. How do I do this?
I load in the data using 'Cesium.CzmlDataSource.load' as shown below. I'm also able to attach a new field like 'model' but not access the position field.
CZML file
[{
"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' };
});
}());
By the time your CZML is imported as entities, those original positions have been converted. The entity.position
object you're accessing is not an array, it's an instance of a SampledPositionProperty
.
This property doesn't publicly expose all of its internal data, but you can request a position at a given time, using position.getValue(...)
.
Here's a Sandcastle Demo showing an entity position changing over time.
The code for that demo looks like this:
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";
}
});
}
});