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.
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));
});