Search code examples
javascriptcesiumjs

How to get scene change events in cesium?


I'd like to detect every time the camera position, heading, pitch or roll changes on Cesium view so that I can update a display showing these values. After quite a bit of searching, I eventually discovered that I can add an event handler to the not-at-all-intuitive preRender or postRender events on the widget's Scene object. However, those events both fire continuously, hundreds of times per second. I would guess they are firing once per clock tick. Is there another event I can register for that will simply fire after the view of the map has been changed? I'm looking for something close to Leaflet's moveend event and preRender and postRender aren't it.

Failing that, is there any way I can get preRender or postRender to fire only when something has actually changed?


Solution

  • I would recommend one of the following solutions:

    • set an interval that is polling the camera position at whatever frequency makes sense for your application.

      var intervalHandle = setInterval(function() {
        var camera = viewer.scene.camera,
          store = {  position: camera.position.clone(),
                     direction: camera.direction.clone(),
                     up: camera.up.clone(),
                     right: camera.right.clone(),
                     transform: camera.transform.clone(),
                     frustum: camera.frustum.clone()
                  };
        //update UI elements
        ...
       }, 1000); // every 1 second whatever is best for your application
      ...
      
    • Attach an event trigger on the instigator of the change. If a mouse, then on mouseup. If keyboard, attach on keyup.

      The CameraEventAggregator object has a member called "anyButtonDown" which would be useful for mouse state changes.

    • You may also be able to use the Camera's FlightCompleteCallback as well. That should trigger whenever the camera is done moving if you used the helper methods to move it.