Select to view content in your preferred language

Draw toolbar events

7749
10
Jump to solution
05-20-2015 06:16 AM
SarahNoakes1
Frequent Contributor

Hi,

I am using the Draw toolbar to allow the user to draw a polygon on the map.  I want to identify when the user has started a new polygon, but the toolbar only fires two events: draw-complete and draw-end - there doesn't seem to be a draw-start event.  However I know there must be something behind the scenes because the default tooltip is "Click to start drawing" before the polygon has been started, "Click to continue drawing" once the first point is set and "Double-click to complete" for all subsequent points.

Any ideas how I can grab hold of the event fired when the first point is clicked?

Thanks,

Sarah Noakes

Cornwall Council.

0 Kudos
10 Replies
KenDoman
Frequent Contributor

Hi Sarah,

After trying a few different methods, the best way I've been able to implement what you're describing without an onDrawBegin event is to use a dojo/on.pausable() event that goes off on the map.onmousedown event, pause it, and only unpause it when the draw-end event occurs.  Here's how it would look:

require([..., "esri/map", "dojo/on", "esri/toolbars/draw", ...], function (..., Map, dojoOn, DrawToolbar, ...) {

  ...  

  var map = new Map("mapdiv", {...});

  var drawtoolbar = new DrawToolbar(map, {...});

  ...

  var clearGraphicsHandler = dojoOn.pausable(map, "mousedown", function () {

   // clear the map graphics layer

    map.graphics.clear();

    // test if you can pause the clearGraphicsHandler after the event is called.

    if (clearGraphicsHandler & typeof clearGraphicsHandler.pause === "function") {

      clearGraphicsHandler.pause();

    }

  });

  // pause it for now

  clearGraphicsHandler.pause();

  ...

  dojoOn(drawtoolbar, "draw-end", function (evt) {

      // do something to add the drawing on the map.

     ...

      // resume the clearGraphicsHandler when you touch the map to draw again.

      clearGraphicsHandler.resume();

  });

  ...

  // remember if you switch away from this tool, and want to preserve the current graphic, be sure to call clearGraphicsHandler.pause();

  ...

});

There's more things you could do to make this more robust. Hopefully, this helps.