require(["esri/views/draw/PolylineDrawAction"], (PolylineDrawAction) => { /* code goes here */ });
import PolylineDrawAction from "@arcgis/core/views/draw/PolylineDrawAction.js";
esri/views/draw/PolylineDrawAction
This class uses different events to generate a set of vertices to create a new Polyline geometry using Draw. When draw.create("polyline") is called, a reference to this class 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
function enableCreatePolyline(draw, view) {
let action = draw.create("polyline");
// listen to PolylineDrawAction.vertex-add
// Fires when the user clicks, or presses the "F" key
// Can also fire when the "R" key is pressed to redo.
action.on("vertex-add", function (evt) {
createPolylineGraphic(evt.vertices);
});
// listen to PolylineDrawAction.vertex-remove
// Fires when the "Z" key is pressed to undo the
// last added vertex
action.on("vertex-remove", function (evt) {
createPolylineGraphic(evt.vertices);
});
// listen to PolylineDrawAction.cursor-update
// fires when the pointer moves over the view
action.on("cursor-update", function (evt) {
createPolylineGraphic(evt.vertices);
});
// listen to PolylineDrawAction.draw-complete
// event to create a graphic when user double-clicks
// on the view or presses the "Enter" key
action.on("draw-complete", function (evt) {
createPolylineGraphic(evt.vertices);
});
}
function createPolylineGraphic(vertices){
view.graphics.removeAll();
let polyline = {
type: "polyline", // autocasts as Polyline
paths: vertices,
spatialReference: view.spatialReference
};
let graphic = new Graphic({
geometry: polyline,
symbol: {
type: "simple-line", // autocasts as SimpleLineSymbol
color: [4, 90, 141],
width: 3,
cap: "round",
join: "round"
}
});
view.graphic.add(graphic);
}
Constructors
-
Parameterproperties Objectoptional
See the properties for a list of all the properties that may be passed into the constructor.
Property Overview
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. | PolylineDrawAction | ||
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
-
hasZ
InheritedPropertyhasZ Boolean
Inherited from DrawAction -
Controls whether the created geometry will have z coordinates or not.
- Default Value:true
-
mode
mode String
Since: ArcGIS Maps SDK for JavaScript 4.7PolylineDrawAction 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"
Exampledraw.create("polyline", {mode: "freehand"});
-
Inherited from DrawAction
-
Two-dimensional array of numbers representing the coordinates of each vertex comprising the geometry being drawn.
-
Inherited from DrawAction
-
A reference to the MapView.
Method Overview
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 polyline geometry and fires the draw-complete event. | PolylineDrawAction | ||
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
-
Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.25Accessor 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();
ParametershandleOrHandles WatchHandle|WatchHandle[]Handles marked for removal once the object is destroyed.
groupKey *optionalKey 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
InheritedMethodcanRedo(){Boolean}
Inherited from DrawAction -
Indicates if the redo() method can be called on the action instance.
Returns
-
canUndo
InheritedMethodcanUndo(){Boolean}
Inherited from DrawAction -
Indicates if the undo() method can be called on the action instance.
Returns
-
Completes drawing the polyline 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
InheritedMethodemit(type, event){Boolean}
Inherited from DrawAction -
Emits an event on the instance. This method should only be used when creating subclasses of this class.
ReturnsType Description Boolean true
if a listener was notified
-
getCoordsAndPointFromScreenPoint
InheritedMethodgetCoordsAndPointFromScreenPoint(screenPoint){FromScreenPointResult |null}
Inherited from DrawAction -
Maps the given screen point to a map point.
ParameterscreenPoint ScreenPointThe location on the screen.
ReturnsType Description FromScreenPointResult | null The result object containing, or null
if the screen point could not be mapped.
-
Inherited from DrawAction
-
Maps the given screen point to a map point.
ParameterscreenPoint ScreenPointThe location on the screen.
Returns
-
hasEventListener
InheritedMethodhasEventListener(type){Boolean}
Inherited from DrawAction -
Indicates whether there is an event listener on the instance that matches the provided event name.
Parametertype StringThe name of the event.
ReturnsType Description Boolean Returns true if the class supports the input event.
-
hasHandles
InheritedMethodhasHandles(groupKey){Boolean}
Inherited from AccessorSince: ArcGIS Maps SDK for JavaScript 4.25Accessor since 4.0, hasHandles added at 4.25. -
Returns true if a named group of handles exist.
ParametergroupKey *optionalA group key.
ReturnsType 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
InheritedMethodon(type, listener){Object}
Inherited from DrawAction -
Registers an event handler on the instance. Call this method to hook an event with a listener.
ParametersReturnsType 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. Exampleview.on("click", function(event){ // event is the event handle returned after the event fires. console.log(event.mapPoint); });
-
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.
Exampleif (action.canRedo()) { action.redo(); }
-
Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.25Accessor since 4.0, removeHandles added at 4.25. -
Removes a group of handles owned by the object.
ParametergroupKey *optionalA group key or an array or collection of group keys to remove.
Exampleobj.removeHandles(); // removes handles from default group obj.removeHandles("handle-group"); obj.removeHandles("other-handle-group");
-
Inherited from DrawAction
-
Maps the given screen point to a map point.
ParameterscreenPoint ScreenPointThe location on the screen.
Returns
-
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.
Exampleif (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. |
PolylineDrawAction |
|
{vertices: Number[][],preventDefault: Function,defaultPrevented: Boolean,type: "draw-complete"} |
Fires after the user has completed drawing a polyline. |
PolylineDrawAction |
|
{vertices: Number[][],vertexIndex: Number,preventDefault: Function,defaultPrevented: Boolean,type: String} |
Fires in response to redo action or when redo() is called. |
PolylineDrawAction |
|
{vertices: Number[][],vertexIndex: Number,preventDefault: Function,defaultPrevented: Boolean,type: String} |
Fires in response to undo action or when undo() is called. |
PolylineDrawAction |
|
{vertices: Number[][],vertexIndex: Number,preventDefault: Function,defaultPrevented: Boolean,type: "vertex-add"} |
Fires when a vertex is added. |
PolylineDrawAction |
|
{vertices: Number[][],vertexIndex: Number,preventDefault: Function,defaultPrevented: Boolean,type: "vertex-remove"} |
Fires when a vertex is removed. |
PolylineDrawAction |
Event Details
-
Fires after the pointer moves on the view.
- Properties
-
Two-dimensional array of numbers representing the coordinates of each vertex that make the geometry.
vertexIndex NumberIndex of the last vertex added to the vertices array.
preventDefault FunctionPrevents event propagation bubbling up the event chain.
defaultPrevented BooleanSet to true when
preventDefault()
is called.type StringThe type of the event.
The value is always "cursor-update".
Example// Give a visual feedback to the user as the pointer moves on the view. action.on("cursor-update", function (evt) { view.graphics.removeAll(); let polyline = new Polyline({ paths: evt.vertices, spatialReference: view.spatialReference }); let graphic = createGraphic(polyline); view.graphics.add(graphic); });
-
Fires after the user has completed drawing a polyline.
- Properties
-
Two-dimensional array of numbers representing the coordinates of each vertex that make the geometry.
preventDefault FunctionPrevents event propagation bubbling up the event chain.
defaultPrevented BooleanSet to true when
preventDefault()
is called.type StringThe type of the event.
The value is always "draw-complete".
Example// listen to PolylineDrawAction.draw-complete // add the graphic representing the completed // polyline to the view action.on("draw-complete", function (evt) { removeGraphic(graphic); let polyline = new Polyline({ paths: evt.vertices, spatialReference: view.spatialReference }); graphic = createGraphic(polyline); view.graphics.add(graphic); });
-
Since: ArcGIS Maps SDK for JavaScript 4.7PolylineDrawAction since 4.5, redo added at 4.7. -
Fires in response to redo action or when redo() is called.
- Properties
-
Two-dimensional array of numbers representing the coordinates of each vertex that make the geometry.
vertexIndex NumberIndex of the vertex where
redo
was applied.preventDefault FunctionPrevents event propagation bubbling up the event chain.
defaultPrevented BooleanSet to true when
preventDefault()
is called.type StringThe 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 ofredo
.
Example// Update the graphic on the view as the last action was redone action.on("redo", function (evt) { view.graphics.removeAll(); let polyline = new Polyline({ paths: evt.vertices, spatialReference: view.spatialReference }); let graphic = createGraphic(polyline); view.graphics.add(graphic); });
-
Since: ArcGIS Maps SDK for JavaScript 4.7PolylineDrawAction since 4.5, undo added at 4.7. -
Fires in response to undo action or when undo() is called.
- Properties
-
Two-dimensional array of numbers representing the coordinates of each vertex that make the geometry.
vertexIndex NumberIndex of the vertex where
undo
was applied.preventDefault FunctionPrevents event propagation bubbling up the event chain.
defaultPrevented BooleanSet to true when
preventDefault()
is called.type StringThe 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 ofundo
.
Example// Update the graphic on the view as the last action was undone action.on("undo", function (evt) { view.graphics.removeAll(); let polyline = new Polyline({ paths: evt.vertices, spatialReference: view.spatialReference }); let graphic = createGraphic(polyline); view.graphics.add(graphic); });
-
Fires when a vertex is added.
- Properties
-
Two-dimensional array of numbers representing the coordinates of each vertex that make the geometry.
vertexIndex NumberIndex of the last vertex added to the vertices array.
preventDefault FunctionPrevents event propagation bubbling up the event chain.
defaultPrevented BooleanSet to true when
preventDefault()
is called.type StringThe type of the event.
The value is always "vertex-add".
Example// fires when a vertex is added. action.on("vertex-add", function (evt) { view.graphics.removeAll(); let polyline = new Polyline({ paths: evt.vertices, spatialReference: view.spatialReference }); let graphic = createGraphic(polyline); view.graphics.add(graphic); });
-
Fires when a vertex is removed.
- Properties
-
Two-dimensional array of numbers representing the coordinates of each vertex that make the geometry.
vertexIndex NumberIndex of the vertex removed from the vertices array.
preventDefault FunctionPrevents event propagation bubbling up the event chain.
defaultPrevented BooleanSet to true when
preventDefault()
is called.type StringThe type of the event.
The value is always "vertex-remove".
Example// Update the graphic on the view as vertex is removed action.on("vertex-remove", function (evt) { view.graphics.removeAll(); let polyline = new Polyline({ paths: evt.vertices, spatialReference: view.spatialReference }); let graphic = createGraphic(polyline); view.graphics.add(graphic); });