I am using the sketch widget for ArcGIS JavaScript: https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Sketch.html
There is a "delete" button after you have selected one or more graphics. Is there a way to trigger the delete operation through the API?
I don't see one in the documentation.
3 Answers 3
There's no method on the Sketch widget to delete a particular or selected graphic.
One posible option is to do it manually using the updateGraphics property of the widget which is a Collection of the graphics that are being edited/selected in that moment.
With that information we can use the method removeMany() from the "graphics" property(Collection) of your GraphicsLayer to remove them.
...
function deleteGraphics() {
const selectedGraphics = sketchWidget.updateGraphics; //Get selected graphics from the widget
if (selectedGraphics) {
graphicsLayer.graphics.removeMany(selectedGraphics); //We remove them from our layer
}
sketchWidget.complete(); //We "complete" the "update" event, setting updateGraphics to empty
}
...
If someone else has a better way to do it I will be happy if its posted, since I'm new to the ArcGIS JS API.
You have to remove the graphics from layer attached to the Sketch widget : https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Sketch.html#layer
const graphicsLayer = sketch.layer;
graphicsLayer.removeAll();
-
Thanks, does that remove all of the graphics or only the selected graphics?Mike Horn– Mike Horn2019年10月14日 15:38:59 +00:00Commented Oct 14, 2019 at 15:38
As of version 4.14, there is now a delete
method on the Sketch class.
sketch.delete();
This will delete the selected graphics associated with the Sketch instance.
https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Sketch.html#delete