This sample shows how to use hitTest to find all objects under the mouse pointer for each click-interaction. Starting at version 4.11, the hitTest in SceneView returns an array of all objects that intersect the mouse location from these layers:
- GraphicsLayer
- FeatureLayer
- SceneLayer
- BuildingSceneLayer
- PointCloudLayer
- CSVLayer
- StreamLayer
- GeoJSONLayer
- SceneView.graphics
If the graphics have non-draped IconSymbol3DLayer symbols, then only the first graphic will be returned from the hitTest.
For each object, the HitTestResult contains the graphic, distance from the camera to the object, and the corresponding map point. Information about where the ground surface (if at all) was hit is also provided.
To initiate the hitTest:
view.on("immediate-click", (event) => {
view.hitTest(event)
.then((hitTestResult) => {
console.log(hitTestResult)
})
.catch((error) => {
console.error(error);
});
};
Example of a HitTestResult:
{
"screenPoint": {
"x": 1110.666748046875,
"y": 704.6666870117188
},
"results": [
{
"graphic": {
"geometry": null,
"symbol": null,
"attributes": {
"OBJECTID": 23259,
"CNSTRCT_YR": 1950
}
},
"mapPoint": {
"spatialReference": {
"latestWkid": 3857,
"wkid": 102100
},
"x": -8237578.175357298,
"y": 4972677.452421391,
"z": 29.343255893327296
},
"distance": 6033.217135564263
}
],
"ground": {
"mapPoint": {
"spatialReference": {
"latestWkid": 3857,
"wkid": 102100
},
"x": -8237575.894557083,
"y": 4972679.5403643185,
"z": 6.689946555570758
},
"distance": 6055.99127013477
}
}