Add listeners to objects created by sketch

500
5
Jump to solution
06-01-2022 03:28 AM
Aeseir
by
Occasional Contributor

I am using Sketch widget to reduce overhead on some items and it mostly works (mostly).

 

This is how i set it up:

 

let sketch = new Sketch({
      availableCreateTools: ["point", "polyline", "polygon"],
      layer: this.graphicsLayer,
      view: this.view,
      creationMode: "single",
      defaultUpdateOptions: {
        enableRotation: false,
        enableScaling: false,
        enableZ: false,
        toggleToolOnClick: false,
      },
      visibleElements: {
        settingsMenu: false,
        selectionTools: {
          "lasso-selection": false,
          "rectangle-selection": false
        }
      }
    });
 
This results in me being able to create polygons which is great.
Question is how do i attach listeners to these shapes, so that on click it triggers a specific function/code?
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
BenElan
Esri Contributor
5 Replies
AdrianaPaese
Occasional Contributor

I suspect the answer from @BenElan to the post 

“SketchViewModel methods turning off layerList toogle” in the Calcite Design System Questions

may help you

0 Kudos
Aeseir
by
Occasional Contributor

How does this help attach listeners to shapes created through Sketch widget?

0 Kudos
AdrianaPaese
Occasional Contributor
Ok
I don´t know which function you are trying to trigger but in the example below, the output Sketch geometry is used as a parameter (sketchEvent.graphics[0].geometry) to a query..
 
 
          sketch.on("update", (sketchEvent) => {

                    const sketck_action_id = sketckEvent.action.id;
                   
                    if (sketchEvent.state === "start") {

                        queryFeatureLayer(sketchEvent.graphics[0].geometry);
                    }
                    if (sketchEvent.state === "complete"){
                        graphicsLayer.remove(sketchEvent.graphics[0]);
                        if (sketchEvent.toolEventInfo && (sketchEvent.toolEventInfo.type === "scale-stop" || sketchEvent.toolEventInfo.type === "reshape-stop" || sketchEvent.toolEventInfo.type === "move-stop")) {
                            queryFeatureLayer(sketchEvent.graphics[0].geometry);
                        }
                    }
                });
0 Kudos
Aeseir
by
Occasional Contributor

Sorry i might not be clear with what i am trying to achieve.

Lets assume i have a bunch of graphics on the map, in this case they were added in by Sketch.

Is it possible to attach a listener to each graphic, so that when clicked on it alerts("hello world").

 

I am fairly new to ArcgJS and the documentation is not as clear as i am used to.

0 Kudos
BenElan
Esri Contributor

The MapView's hitTest method is what you're looking for, here is a sample.