Esri JavaScript 4x GraphicsLayer on click event

7267
1
Jump to solution
12-27-2018 09:47 AM
DanielWebb2
New Contributor III

With JS 3x, I was able to do an onclick event on GraphicsLayer. This came is handy for moving the points somewhere else, etc. Is there a way to do this in JS4x?  Or is this functionality planned for the future?

Thank you!

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Daniel,

  View.hitTest is how this is handled in the 4.x API.

// Get the screen point from the view's click event
view.on("click", function (event) {
 var screenPoint = {
   x: event.x,
   y: event.y
 };

 // Search for graphics at the clicked location
 view.hitTest(screenPoint).then(function (response) {
  if (response.results.length) {
   var graphic = response.results.filter(function (result) {
    // check if the graphic belongs to the layer of interest
    return result.graphic.layer === myLayer;
   })[0].graphic;
   // do something with the result graphic
   console.log(graphic.attributes);
  }
 });
});

See this sample:

Access features with pointer events | ArcGIS API for JavaScript 4.10 

View solution in original post

1 Reply
RobertScheitlin__GISP
MVP Emeritus

Daniel,

  View.hitTest is how this is handled in the 4.x API.

// Get the screen point from the view's click event
view.on("click", function (event) {
 var screenPoint = {
   x: event.x,
   y: event.y
 };

 // Search for graphics at the clicked location
 view.hitTest(screenPoint).then(function (response) {
  if (response.results.length) {
   var graphic = response.results.filter(function (result) {
    // check if the graphic belongs to the layer of interest
    return result.graphic.layer === myLayer;
   })[0].graphic;
   // do something with the result graphic
   console.log(graphic.attributes);
  }
 });
});

See this sample:

Access features with pointer events | ArcGIS API for JavaScript 4.10