Skip to content

Scene - hitTest

Use viewElement.hitTest() to get all graphics or features under the pointer at the moment of interaction. The method returns an ordered array (nearest first) of intersections from these sources: GraphicsLayer, FeatureLayer, SceneLayer, BuildingSceneLayer, PointCloudLayer, CSVLayer, StreamLayer, GeoJSONLayer, viewElement.graphics

What you get back (HitTestResult)

Each hit returns:

  • graphic – the matched graphic or feature (with attributes, if present)
  • mapPoint – the 3D point on the object
  • distance – camera-to-hit distance (in view units)

If the ground is intersected, ground.mapPoint and ground.distance are also provided.

To initiate the hitTest:

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
viewElement.addEventListener("arcgisViewImmediateClick", (event) => {
  viewElement.hitTest(event.detail)
  .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.