How to zoom to added layer automatically while selecting that layer?

Hello, I am new to CesiumJS as well as JS. I added few layers in my Cesium viewer through ‘on select’ function. I am not able to zoom to the selected layer on selection. Is there a way in which the Camera flies automatically to the selected layer, without specifying the rectangle variable or destination coordinates in the viewer.camera.flyTo() function ?

Hiya,

Yes, plenty of options. When you say “layer”, what exactly is it? There’s some extra stuff you have to do if it’s, say, an ImageryLayer, but if it’s an entity or model the easiest is viewer.flyTo(myThing).

Cheers,

Alex

Hey @Alexander_Johannesen ,

I am adding raster imagery layers, serving them through geoserver. The syntax of the code goes as:

var imageryLayers = viewer.imageryLayers;
imageryLayers.addImageryProvider(
new Cesium.WebMapServiceImageryProvider({
url: “http://localhost:8585/geoserver/wms”,
layers: "cite:20211125 ",
parameters: {
transparent: true,
format: “image/png”
}})
);
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(76.77, 1.33, 4600000),
orientation: {
heading: Cesium.Math.toRadians(0),
pitch: Cesium.Math.toRadians(-65.0),
roll: 0.0,
}

In that case you need to get the bounding rectangle from it, something like;

imageLayer.getViewableRectangle().then(function (rectangle) {
var bounds = Cesium.Rectangle.subsample(rectangle, Cesium.Ellipsoid.WGS84, 20.0 );
viewer.flyTo(bounds);
}

You may have to look up the documentation on those functions a bit closer, I’m going from memory here (in the car at the moment :slight_smile: ). If these are images with an actual bound, it works fine, however global maps are obviously pointless and I’m not sure where it will fly you …

Cheers,

Alex

@Alexander_Johannesen, I am using tileset for thousands of buildings, How can I zoom in on particular building of tileset using query (suppose i’ll use building id).
Can I get bounding rectangle of particular building of a tileset?

Ok, so if it’s one model for thousands of buildings it gets a bit trickier, but not impossible. All tilesets have a structure to them, depending on how they are saved. I would start at the Tileset.root sub-object and traverse through there to find the bounds of further sub-groups. You could also poke your head into Tileset.tilesLoaded() and Tileset.allTilesLoaded() events and keep track of tiles in (and out) of view with bounding spheres (to some degree of the structure in your tileset file).

Hard to give further advice without more info, but maybe create an example of logging the above events. I remember back a year or so ago I did an automatic bounds tracker plugin for pointclouds, but the principle is the same.

Cheers,

Alex

@ Alexander_Johannesen Thank you so much for your valuable reply. I’ll try the way you have explained.