Search code examples
javascriptcesiumjs

Capture zoom out event in Cesium


I want to capture zoom out event as soon as user reduces the map size to an extent i have to change the Map image layer.

var viewer = new Cesium.Viewer("cesiumContainer");
var scene = viewer.scene;
var clock = viewer.clock;
var referenceFramePrimitive;
var camera = viewer.camera;
....
camera.changed.addEventListener(function()
   {
    var height = Cesium.Cartographic.fromCartesian(camera.position).height;
    if(height<4251907)
    {
      var layers = viewer.imageryLayers;
      var baseLayer = layers.get(0);
      layers.remove(baseLayer);
      layers.addImageryProvider(new Cesium.IonImageryProvider({ assetId: 3812,  maximumLevel : 5 }));
    }
  console.log(height);
}.bind(camera));

Is there any way to achieve this.

Thanks


Solution

  • This is one of the very simple solutions.

        const viewer = new Cesium.Viewer("cesiumContainer");
    
        const camera = viewer.camera;
    
        const scratchCartesian1 = new Cesium.Cartesian3();
        const scratchCartesian2 = new Cesium.Cartesian3();
    
        let startPos, endPos;
    
        camera.moveStart.addEventListener(function () {
            startPos = camera.positionWC.clone(scratchCartesian1);
    
        });
    
        camera.moveEnd.addEventListener(function () {
            endPos = camera.positionWC.clone(scratchCartesian2);
    
            const startHeight = Cesium.Cartographic.fromCartesian(startPos).height;
            const endHeight = Cesium.Cartographic.fromCartesian(endPos).height;
    
            if (startHeight > endHeight) {
                console.log("zoom in");
            } else {
                console.log("zoom out");
            }
    
        });