SceneView - hitTest

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:

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:

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
view.on("immediate-click", (event) => {
  view.hitTest(event)
  .then((hitTestResult) => {
    console.log(hitTestResult)
  })
  .catch((error) => {
    console.error(error);
  });
};

Example of a HitTestResult:

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{
  "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
  }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.