Skip to content

This sample shows how to query a mesh SceneLayerView to get more information about features visible in the view. The SceneLayerView query methods only execute queries against features that are visible in the view at the moment of the query.

The use case for this sample is to display more information about a building and to zoom to it when the user clicks on that building.

We can get the objectid of the feature the user clicked using the hitTest() method of the Scene. We use the result to create a query object:

// Get the response from the hitTest and check
// if a graphic is returned from the first result
const response = await viewElement.hitTest(event.detail);
const graphic = response.results[0]?.graphic;
if (!graphic) return;
// Create query object: by specifying objectIds, the query will return results only for
// the feature matching the graphic's objectid
const query = new Query({
objectIds: [graphic.attributes.OBJECTID],
outFields: ["*"], // return all attributes
});
// check if a graphic is returned from the hitTest
if (response.results[0].graphic) {
// create query
const query = new Query();
// the query will return results only for the feature with the graphic's objectid
query.objectIds = [response.results[0].graphic.attributes.OBJECTID];
// the query should return all attributes
query.outFields = ["*"];

The query object is then passed to the queryExtent() method, which returns the 3D extent of the feature the camera should go to.

//queryExtent() returns the extent of the feature that satisfies the `query`
const result = await sceneLayerView.queryExtent(query);
await viewElement.goTo(
{
target: result.extent.expand(2),
tilt: 60,
},
{
duration: 1000,
easing: "expo-out",
},
);

Additional scene layer query samples