PolygonDrawAction

AMD: require(["esri/views/draw/PolygonDrawAction"], (PolygonDrawAction) => { /* code goes here */ });
ESM: import PolygonDrawAction from "@arcgis/core/views/draw/PolygonDrawAction.js";
Class: esri/views/draw/PolygonDrawAction
Inheritance: PolygonDrawActionDrawActionAccessor
Since: ArcGIS Maps SDK for JavaScript 4.5

This class uses different events to generate a set of vertices to create a new Polygon geometry using Draw. When draw.create("polygon") is called, a reference to PolygonDrawAction is returned. You can listen to events on the PolylineDrawAction instance, which allows users to create polylines that meet criteria specified by the developer.

See also
Example
function enableCreatePolygon(draw, view) {
  let action = draw.create("polygon");

  // PolygonDrawAction.vertex-add
  // Fires when user clicks, or presses the "F" key.
  // Can also be triggered when the "R" key is pressed to redo.
  action.on("vertex-add", function (evt) {
    createPolygonGraphic(evt.vertices);
  });

  // PolygonDrawAction.vertex-remove
  // Fires when the "Z" key is pressed to undo the last added vertex
  action.on("vertex-remove", function (evt) {
    createPolygonGraphic(evt.vertices);
  });

  // Fires when the pointer moves over the view
  action.on("cursor-update", function (evt) {
    createPolygonGraphic(evt.vertices);
  });

  // Add a graphic representing the completed polygon
  // when user double-clicks on the view or presses the "Enter" key
  action.on("draw-complete", function (evt) {
    createPolygonGraphic(evt.vertices);
  });
}

function createPolygonGraphic(vertices){
  view.graphics.removeAll();
  let polygon = {
    type: "polygon", // autocasts as Polygon
    rings: vertices,
    spatialReference: view.spatialReference
  };

  let graphic = new Graphic({
    geometry: polygon,
    symbol: {
      type: "simple-fill", // autocasts as SimpleFillSymbol
      color: "purple",
      style: "solid",
      outline: {  // autocasts as SimpleLineSymbol
        color: "white",
        width: 1
      }
    }
  });
  view.graphics.add(graphic);
}

Constructors

PolygonDrawAction

Constructor
new PolygonDrawAction(properties)
Parameter
properties Object
optional

See the properties for a list of all the properties that may be passed into the constructor.

Property Overview

Any properties can be set, retrieved or listened to. See the Working with Properties topic.
Show inherited properties Hide inherited properties
Name Type Summary Class

The name of the class.

Accessor

Controls whether the created geometry will have z coordinates or not.

DrawAction

The drawing mode.

PolygonDrawAction

Two-dimensional array of numbers representing the coordinates of each vertex comprising the geometry being drawn.

DrawAction

A reference to the MapView.

DrawAction

Property Details

declaredClass

Inherited
Property
declaredClass Stringreadonly
Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.7 Accessor since 4.0, declaredClass added at 4.7.

The name of the class. The declared class name is formatted as esri.folder.className.

hasZ

Inherited
Property
hasZ Boolean
Inherited from DrawAction

Controls whether the created geometry will have z coordinates or not.

Default Value:true

mode

Property
mode String
Since: ArcGIS Maps SDK for JavaScript 4.7 PolygonDrawAction since 4.5, mode added at 4.7.

The drawing mode. It is only relevant when the action is first created. Its value cannot be changed during the action lifecycle.

Possible Values

Value Description
hybrid Vertices are added while the pointer is clicked or dragged.
freehand Vertices are added while the pointer is dragged.
click Vertices are added when the pointer is clicked.

Possible Values:"hybrid" |"freehand" |"click"

Default Value:"hybrid"
Example
draw.create("polygon", {mode: "freehand"});

vertices

Inherited
Property
vertices Number[][]readonly
Inherited from DrawAction

Two-dimensional array of numbers representing the coordinates of each vertex comprising the geometry being drawn.

view

Inherited
Property
view MapView |SceneView
Inherited from DrawAction

A reference to the MapView.

Method Overview

Show inherited methods Hide inherited methods
Name Return Type Summary Class

Adds one or more handles which are to be tied to the lifecycle of the object.

Accessor

Indicates if the redo() method can be called on the action instance.

DrawAction

Indicates if the undo() method can be called on the action instance.

DrawAction

Completes drawing the polygon geometry and fires the draw-complete event.

PolygonDrawAction

Emits an event on the instance.

DrawAction

Maps the given screen point to a map point.

DrawAction

Maps the given screen point to a map point.

DrawAction

Indicates whether there is an event listener on the instance that matches the provided event name.

DrawAction

Returns true if a named group of handles exist.

Accessor

Registers an event handler on the instance.

DrawAction

Incrementally redo actions recorded in the stack.

DrawAction

Removes a group of handles owned by the object.

Accessor

Maps the given screen point to a map point.

DrawAction

Incrementally undo actions recorded in the stack.

DrawAction

Method Details

addHandles

Inherited
Method
addHandles(handleOrHandles, groupKey)
Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.25 Accessor since 4.0, addHandles added at 4.25.

Adds one or more handles which are to be tied to the lifecycle of the object. The handles will be removed when the object is destroyed.

// Manually manage handles
const handle = reactiveUtils.when(
  () => !view.updating,
  () => {
    wkidSelect.disabled = false;
  },
  { once: true }
);

this.addHandles(handle);

// Destroy the object
this.destroy();
Parameters
handleOrHandles WatchHandle|WatchHandle[]

Handles marked for removal once the object is destroyed.

groupKey *
optional

Key identifying the group to which the handles should be added. All the handles in the group can later be removed with Accessor.removeHandles(). If no key is provided the handles are added to a default group.

canRedo

Inherited
Method
canRedo(){Boolean}
Inherited from DrawAction

Indicates if the redo() method can be called on the action instance.

Returns
Type Description
Boolean Returns true if the redo() method can be called.

canUndo

Inherited
Method
canUndo(){Boolean}
Inherited from DrawAction

Indicates if the undo() method can be called on the action instance.

Returns
Type Description
Boolean Returns true if the undo() method can be called.

complete

Method
complete()

Completes drawing the polygon geometry and fires the draw-complete event. Call this method if the drawing logic needs to be completed other than by double-clicking or pressing the "Enter" key.

emit

Inherited
Method
emit(type, event){Boolean}
Inherited from DrawAction

Emits an event on the instance. This method should only be used when creating subclasses of this class.

Parameters
type String

The name of the event.

event Object
optional

The event payload.

Returns
Type Description
Boolean true if a listener was notified

getCoordsAndPointFromScreenPoint

Inherited
Method
getCoordsAndPointFromScreenPoint(screenPoint){FromScreenPointResult |null}
Inherited from DrawAction

Maps the given screen point to a map point.

Parameter
screenPoint ScreenPoint

The location on the screen.

Returns
Type Description
FromScreenPointResult | null The result object containing, or null if the screen point could not be mapped.

getCoordsFromScreenPoint

Inherited
Method
getCoordsFromScreenPoint(screenPoint){Number[] |null}
Inherited from DrawAction

Maps the given screen point to a map point.

Parameter
screenPoint ScreenPoint

The location on the screen.

Returns
Type Description
Number[] | null Array of x,y and z component (if hasZ is enabled) of the point associated with the given screen point or null if screen point could not be mapped.

hasEventListener

Inherited
Method
hasEventListener(type){Boolean}
Inherited from DrawAction

Indicates whether there is an event listener on the instance that matches the provided event name.

Parameter
type String

The name of the event.

Returns
Type Description
Boolean Returns true if the class supports the input event.

hasHandles

Inherited
Method
hasHandles(groupKey){Boolean}
Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.25 Accessor since 4.0, hasHandles added at 4.25.

Returns true if a named group of handles exist.

Parameter
groupKey *
optional

A group key.

Returns
Type Description
Boolean Returns true if a named group of handles exist.
Example
// Remove a named group of handles if they exist.
if (obj.hasHandles("watch-view-updates")) {
  obj.removeHandles("watch-view-updates");
}

on

Inherited
Method
on(type, listener){Object}
Inherited from DrawAction

Registers an event handler on the instance. Call this method to hook an event with a listener.

Parameters

An event or an array of events to listen for.

listener Function

The function to call when the event fires.

Returns
Type Description
Object Returns an event handler with a remove() method that should be called to stop listening for the event(s).
Property Type Description
remove Function When called, removes the listener from the event.
Example
view.on("click", function(event){
  // event is the event handle returned after the event fires.
  console.log(event.mapPoint);
});

redo

Inherited
Method
redo()
Inherited from DrawAction

Incrementally redo actions recorded in the stack. Call canRedo() prior to calling this method to check if this method can be called on the action instance. Calling this method will fire the vertex-add or vertex-remove events depending on the last action.

Example
if (action.canRedo()) {
  action.redo();
}

removeHandles

Inherited
Method
removeHandles(groupKey)
Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.25 Accessor since 4.0, removeHandles added at 4.25.

Removes a group of handles owned by the object.

Parameter
groupKey *
optional

A group key or an array or collection of group keys to remove.

Example
obj.removeHandles(); // removes handles from default group

obj.removeHandles("handle-group");
obj.removeHandles("other-handle-group");

screenToMap

Inherited
Method
screenToMap(screenPoint){Point |null}
Inherited from DrawAction

Maps the given screen point to a map point.

Parameter
screenPoint ScreenPoint

The location on the screen.

Returns
Type Description
Point | null MapPoint associated with the given screen point or null if screen point could not be mapped.

undo

Inherited
Method
undo()
Inherited from DrawAction

Incrementally undo actions recorded in the stack. Call canUndo() prior to calling this method to check if this method can be called on the action instance. Calling this method will fire the vertex-add or vertex-remove events depending on the last action.

Example
if (action.canUndo()) {
  action.undo();
}

Event Overview

Name Type Summary Class
{vertices: Number[][],vertexIndex: Number,preventDefault: Function,defaultPrevented: Boolean,type: "cursor-update"}

Fires after the pointer moves on the view.

PolygonDrawAction
{vertices: Number[][],preventDefault: Function,defaultPrevented: Boolean,type: "draw-complete"}

Fires after the user has completed drawing a polygon.

PolygonDrawAction
{vertices: Number[][],vertexIndex: Number,preventDefault: Function,defaultPrevented: Boolean,type: String}

Fires in response to redo action or when redo() is called.

PolygonDrawAction
{vertices: Number[][],vertexIndex: Number,preventDefault: Function,defaultPrevented: Boolean,type: String}

Fires in response to undo action or when undo() is called.

PolygonDrawAction
{vertices: Number[][],vertexIndex: Number,preventDefault: Function,defaultPrevented: Boolean,type: "vertex-add"}

Fires when a vertex is added.

PolygonDrawAction
{vertices: Number[][],vertexIndex: Number,preventDefault: Function,defaultPrevented: Boolean,type: "vertex-remove"}

Fires when a vertex is removed.

PolygonDrawAction

Event Details

cursor-update

Event
cursor-update

Fires after the pointer moves on the view.

Properties
vertices Number[][]

A two-dimensional array of numbers representing the coordinates of each vertex that comprise the geometry.

vertexIndex Number

Index of the last vertex added to the vertices array.

preventDefault Function

Prevents event propagation bubbling up the event chain.

defaultPrevented Boolean

Set to true when preventDefault() is called.

type String

The type of the event.

The value is always "cursor-update".

Example
// listen to PolygonDrawAction.cursor-update
// give a visual feedback to the user on the view
// as user moving the pointer.
action.on("cursor-update", function (evt) {
  view.graphics.removeAll();
  let polygon = new Polygon({
    rings: evt.vertices,
    spatialReference: view.spatialReference
  });
  graphic = createGraphic(polygon);
  view.graphics.add(graphic);
});

draw-complete

Event
draw-complete

Fires after the user has completed drawing a polygon.

Properties
vertices Number[][]

A two-dimensional array of numbers representing the coordinates of each vertex that comprise the geometry.

preventDefault Function

Prevents event propagation bubbling up the event chain.

defaultPrevented Boolean

Set to true when preventDefault() is called.

type String

The type of the event.

The value is always "draw-complete".

Example
// listen to PolygonDrawAction.draw-complete
// add the graphic representing the completed
// polygon to the view
action.on("draw-complete", function (evt) {
  view.graphics.removeAll();
  let polygon = new Polygon({
    rings: evt.vertices,
    spatialReference: view.spatialReference
  });
  graphic = createGraphic(polygon);
  view.graphics.add(graphic);
});

redo

Event
redo
Since: ArcGIS Maps SDK for JavaScript 4.7 PolygonDrawAction since 4.5, redo added at 4.7.

Fires in response to redo action or when redo() is called.

Properties
vertices Number[][]

Two-dimensional array of numbers representing the coordinates of each vertex that make the geometry.

vertexIndex Number

Index of the vertex where redo was applied.

preventDefault Function

Prevents event propagation bubbling up the event chain.

defaultPrevented Boolean

Set to true when preventDefault() is called.

type String

The type of the event. For this event, the type the event that was redone. For instance, the type would be vertex-add if a vertex was added as a result of redo.

Example
// Update the graphic on the view as the last action was redone
action.on("redo", function (evt) {
  view.graphics.removeAll();
  let polygon = new Polygon({
    rings: evt.vertices,
    spatialReference: view.spatialReference
  });
  graphic = createGraphic(polygon);
  view.graphics.add(graphic);
});

undo

Event
undo
Since: ArcGIS Maps SDK for JavaScript 4.7 PolygonDrawAction since 4.5, undo added at 4.7.

Fires in response to undo action or when undo() is called.

Properties
vertices Number[][]

Two-dimensional array of numbers representing the coordinates of each vertex that make the geometry.

vertexIndex Number

Index of the vertex where undo was applied.

preventDefault Function

Prevents event propagation bubbling up the event chain.

defaultPrevented Boolean

Set to true when preventDefault() is called.

type String

The type of the event. For this event, the type the event that was undone. For instance, the type would be vertex-remove if a vertex was removed as a result of undo.

Example
// Update the graphic on the view as the last action was undone
action.on("undo", function (evt) {
  view.graphics.removeAll();
  let polygon = new Polygon({
    rings: evt.vertices,
    spatialReference: view.spatialReference
  });
  graphic = createGraphic(polygon);
  view.graphics.add(graphic);
});

vertex-add

Event
vertex-add

Fires when a vertex is added.

Properties
vertices Number[][]

A two-dimensional array of numbers representing the coordinates of each vertex that make the geometry.

vertexIndex Number

Index of the last vertex added to the vertices array.

preventDefault Function

Prevents event propagation bubbling up the event chain.

defaultPrevented Boolean

Set to true when preventDefault() is called.

type String

The type of the event.

The value is always "vertex-add".

Example
// listen to PolygonDrawAction.vertex-add
// give a visual feedback to the user on the view
// as user moving the pointer.
action.on("vertex-add", function (evt) {
  view.graphics.removeAll();
  let polygon = new Polygon({
    rings: evt.vertices,
    spatialReference: view.spatialReference
  });
  graphic = createGraphic(polygon);
  view.graphics.add(graphic);
});

vertex-remove

Event
vertex-remove

Fires when a vertex is removed.

Properties
vertices Number[][]

A two-dimensional array of numbers representing the coordinates of each vertex that make the geometry.

vertexIndex Number

Index of the vertex removed from the vertices array.

preventDefault Function

Prevents event propagation bubbling up the event chain.

defaultPrevented Boolean

Set to true when preventDefault() is called.

type String

The type of the event.

The value is always "vertex-remove".

Example
// listen to PolygonDrawAction.vertex-remove
// give a visual feedback to the user on the view
// as user moving the pointer.
action.on("vertex-remove", function (evt) {
  view.graphics.removeAll();
  let polygon = new Polygon({
    rings: evt.vertices,
    spatialReference: view.spatialReference
  });
  graphic = createGraphic(polygon);
  view.graphics.add(graphic);
});

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.