zoomTo does not include all entities in view & changes based on screen size

In 2D Mode, zoom to does not include all entities in view if they are far apart. Zoom extent also changes based on screen size.

How can I ensure that zoomTo always zooms to keep all entities in view and that the result is consistent despite screen size?

Please see images and code below.


var viewer = new Cesium.Viewer("cesiumContainer",{sceneMode: Cesium.SceneMode.SCENE2D} );

var blueBox = viewer.entities.add({
  name: "Blue box",
  position: Cesium.Cartesian3.fromDegrees(-114.0, 40.0),
  box: {
    dimensions: new Cesium.Cartesian3(400000.0, 300000.0, 500000.0),
    material: Cesium.Color.BLUE,
  },
});

var redBox = viewer.entities.add({
  name: "Red box with black outline",
  position: Cesium.Cartesian3.fromDegrees(-63.0, -40.0),
  box: {
    dimensions: new Cesium.Cartesian3(400000.0, 300000.0, 500000.0),
    material: Cesium.Color.RED.withAlpha(0.5),
    outline: true,
    outlineColor: Cesium.Color.BLACK,
  },
});

var outlineOnly = viewer.entities.add({
  name: "Yellow box outline",
  position: Cesium.Cartesian3.fromDegrees(121.0, 12.0),
  box: {
    dimensions: new Cesium.Cartesian3(400000.0, 300000.0, 500000.0),
    fill: false,
    outline: true,
    outlineColor: Cesium.Color.YELLOW,
  },
});

viewer.zoomTo(viewer.entities);

@sarah1

Welcome to the community! :grinning: :confetti_ball:

Do you think that limiting the zoom amount would fix this issue? It seems like the camera is moving too close to the terrain, meaning that only a subset of the entities are visible.

-Sam

Are you referring to doing something similar to this? Zoom to coordinates while keeping a certain height

That could work, and I’m testing it locally, but I haven’t found a good number for range in meters.

It seems like the zoomTo functionality doesn’t work as expected, at least on larger scales. I would expect that zoomTo adjust zoom level based on how far all the targets are, but instead it just zooms to an “arbitrary” level at the center of all the targets, or even one large target. I only say arbitrary because I don’t know exactly how the function determines zoom extent.

@sarah1

The thread that you shared is exactly what I had in mind. I suggest you move forward with the suggestions outlined in that thread and spend some time experimenting with the correct range.

A few miscellaneous questions and thoughts:

  • Is the zoomTo function zooming to a different altitude each time you call it?
  • I recommend looking through our source code for zoomTo. This might provide some clarity on how we are calculating the Camera altitude.
  • Is zoomTo the correct function to use here? Would you be better off moving the Camera to a location manually?

-Sam