graphicslayer onclick

1142
4
07-25-2018 06:25 AM
SamuelHowell
New Contributor

I want to make an event fire upon clicking a GraphicsLayer. I have tried to get this to work with the following code:

var symbolsLayer = new GraphicsLayer();

...

symbolsLayer.on("click", function (event) {
    // event is the event handle returned after the event fires.
    console.log(event.mapPoint);
    debugger;
});

The event is never fired - but there is no error in the console. What is the correct way to do this?

0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Samuel,

   Is this 3.x or 4.x API?

0 Kudos
SamuelHowell
New Contributor

Hi there, I am using the new 4.x API

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Samuel,

   So if you are using 4.x then as Oren mentioned above you have to use hitTest.

      // Set up a click event handler and retrieve the screen point
      view.on("click", function(evt) {
        var screenPoint = evt.screenPoint;

        // the hitTest() checks to see if any graphics in the view
        // intersect the given screen point
        view.hitTest(screenPoint)
          .then(getGraphics);
      });

      function getGraphics(response) {
        // the topmost graphic from the click location
        // and display select attribute values from the
        // graphic to the user
        console.info(response.results);
       
        var graphic = response.results[0].graphic;
        var attributes = graphic.attributes;
...
OrenGal
New Contributor III

Hi, Please try:

myMapView.on("click", function (evt) {
    var screenPoint = {
        x: evt.x,
        y: evt.y
    };
    myMapView.hitTest(screenPoint).then(function (response) {
        $.each(response.results, function (index, re) {
            var id = re.graphic.layer.id;
            if (id === "graphicslayername") {
                //do something
            }
        });
    });
});