cesiumjsczml

How to access entities in CzmlDataSource after loading


I want to access to some entities in my czml data source for keep tracking in the viewer but after the loading, as one of the options for camera. I know that I can access entities in my czml file while I am loading them, but I don't know how I can access to them after loading. Here I have an example:

  var viewer = new Cesium.Viewer('cesiumContainer'); 
  var czmlDataSource = new Cesium.CzmlDataSource();
  viewer.dataSources.add(czmlDataSource);
  czmlDataSource.load('../../SampleData/Vehicle.czml').then(function(){
         var myEntity= czmlDataSource.entities.getById('Vehicle');
          viewer.trackedEntity=myEntity;
         });

This code works fine, but I want to give option to the viewer to choose the camera, then I need to have access to Vehicle after I finished loading, I tried several methods, but non of them works. I have some example bellow:

  var viewer = new Cesium.Viewer('cesiumContainer'); 
  var czmlDataSource = new Cesium.CzmlDataSource();
  viewer.dataSources.add(czmlDataSource);
  czmlDataSource.load('../../SampleData/Vehicle.czml');
  var myEntity= czmlDataSource.entities.getById('Vehicle');
  viewer.trackedEntity=myEntity;

Do you know how I can define an entity from those which are already in my czml file?


Solution

  • The reason your second block of code doesn't work appears to be simply because you haven't waited for the asynchronous load of the czmlDataSource.

    Try modifying your second block of code, take off the last 2 lines and wrap them in a button onClick callback. If you click the button before the CZML is loaded, myEntity will be undefined and the camera won't change. If you click the same button again after the CZML loads, it should work fine.