Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 441
dgvid Avatar asked Mar 25 '15 18:03

dgvid


3 Answers

Another solution that doesn't use a timer is to listen for the cameras move start and move end events.

viewer.camera.moveStart.addEventListener(function() { 
     // the camera started to move
});
viewer.camera.moveEnd.addEventListener(function() { 
     // the camera stopped moving
});

These sound more like the events you are looking for.

[Edit] : While the inertia spin and zoom are enabled by default and look kinda cool, they can be disabled very easily. This will make it so the moveEnd event is fired as expected. Here is how you can disable the inertia.

var viewer = new Cesium.Viewer();

viewer.scene.screenSpaceCameraController.inertiaSpin = 0;
viewer.scene.screenSpaceCameraController.inertiaTranslate = 0;
viewer.scene.screenSpaceCameraController.inertiaZoom = 0;
like image 79
Zac Avatar answered Nov 20 '22 16:11

Zac


Using cesium v1.30, you can use this code:

viewer.camera.changed.addEventListener(function() {
  var deg = Math.round( Cesium.Math.toDegrees(viewer.camera.heading))
  console.log('Heading:', deg)

  var deg = Math.round( Cesium.Math.toDegrees(viewer.camera.pitch))
  console.log('Pitch:', deg)
})
like image 3
Christopher DeFreitas Avatar answered Nov 20 '22 17:11

Christopher DeFreitas


In addition to the answer https://stackoverflow.com/a/42044819/3092437 you probably want to set viewer.camera.percentageChanged to some small value close to 0 (but with 0 value camera tends to generate change events all the time). I use 0.001, it gives pretty good events frequency at all zoom levels.

like image 2
TotalAMD Avatar answered Nov 20 '22 17:11

TotalAMD