Access features with pointer events

This sample shows how to use the hitTest() method on the MapView to access features in a FeatureLayer. This is done by setting up a pointer-move event handler on the view and passing the returned screen x, y coordinates to the hitTest() method of the view, along with additional options to only include graphics from the FeatureLayer. A promise is returned, which resolves to an array of objects containing any features from the FeatureLayer. If a feature is returned, then the sample displays an information pertaining to this feature.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
view.on("pointer-move", (event) => {
  // only include graphics from hurricanesLayer in the hitTest
  const opts = {
    include: hurricanesLayer
  }
  view.hitTest(event, opts).then((response) => {
    // check if a feature is returned from the hurricanesLayer
    if (response.results.length) {
      const graphic = response.results[0].graphic;
      // do something with the graphic
    }
  });
});

The sample displays hurricane paths. Click any line segment to view some of its attributes and assign the same symbol to all line segments with the same storm name.

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