Load new entities when the camera or scene has changed

I’d like to load and add new entities to a CesiumJS scene dynamically, e.g. when the user changed the camera or when flying to a new place. In order to not load to many entities from the backend, I’d like to restrict the updates too when the user is close to the terrain. The user case is too find and discover new entities, e.g. placemarks my exploring the terrain. Placemarks could e.g. be geolocated Wikipedia entries, geotagged photos or other pins.

To the technical parts… I know how to add (and remove) entities to data sources, so ideally I would write a custom data sources. But how would it get an event that the camera (or the scene?) has changed?

And second question would be how to get an approximate rectangle or bounding sphere of what the user currently sees? It must not be exact, since the user can always pan to other interesting parts.

Hi @Michael_Fink , You can use Camera events to get an event if camera changed it’s position. You may go through CameraChanged or CameraMoveEnd event.
You can use computeViewRectangle method to calculate bounding box of current scene.

Example-

const viewer = new Cesium.Viewer("cesiumContainer");

/* Can use this one also
viewer.camera.changed.addEventListener(function() {
  
  var deg = Math.round( Cesium.Math.toDegrees(viewer.camera.heading));
  console.log(`Heading: ${deg}`);

  var pdeg = Math.round( Cesium.Math.toDegrees(viewer.camera.pitch));
  console.log(`Pitch: ${pdeg}`);
  
});

*/

viewer.camera.moveStart.addEventListener(function() { 
  console.log("the camera started to move");
});

viewer.camera.moveEnd.addEventListener(function() { 
  var scratchRectangle = new Cesium.Rectangle();
  var rect = viewer.camera.computeViewRectangle(viewer.scene.globe.ellipsoid,
        scratchRectangle);
  console.log('West: ' + Cesium.Math.toDegrees(rect.west).toFixed(4) + 
        'South: ' + Cesium.Math.toDegrees(rect.south).toFixed(4) + 
        'East: ' + Cesium.Math.toDegrees(rect.east).toFixed(4) + 
        'North: ' + Cesium.Math.toDegrees(rect.north).toFixed(4));
});
  • Regards

Thanks @Jacky, that’s exactly what I was looking for. For some unknown reason I didn’t check out what the camera was offering on events :face_with_peeking_eye: