SketchViewModel

require(["esri/widgets/Sketch/SketchViewModel"], function(SketchViewModel) { /* code goes here */ });
Class: esri/widgets/Sketch/SketchViewModel
Inheritance: SketchViewModel Accessor
Since: ArcGIS API for JavaScript 4.5

SketchViewModel simplifies the process of adding temporary geometries to the MapView. It saves the effort of writing lots of code for sketching different geometry types. SketchViewModel is intended for basic usage, without advanced features such as preventing self-intersecting lines or overlapping polygons. If more control is required for creating temporary geometries, then use the Draw class instead.

SketchViewModel provides the logic for Sketch widget, which will be added in a future release. The Sketch widget will simplify the process of adding temporary geometries to the view with minimal code.

Pointer and keyboard gestures for drawing points, polylines, and polygons are described in the tables below.

Sketching points

GestureAction
ClickAdds a point at the pointer location.
CAdds a point at the pointer location.

Sketching polylines and polygons

GestureAction
ClickAdds a vertex at the pointer location.
DragAdds a vertex for each pointer move.
FAdds a vertex to the polyline or polygon.
CCompletes the polyline or polygon sketch.
ZIncrementally undos actions recorded in the stack.
RIncrementally redos actions recorded in the stack.
See also:
Example:
// create a new sketch view model and
// assign symbols for each geometry type
var sketch = new SketchViewModel({
  view: view,
  pointSymbol: {
    type: "simple-marker",  // autocasts as new SimpleMarkerSymbol()
    style: "square",
    color: "red",
    size: "16px",
    outline: {  // autocasts as new SimpleLineSymbol()
      color: [255, 255, 0],
      width: 3
    }
  },
  polylineSymbol: {
    type: "simple-line",  // autocasts as new SimpleMarkerSymbol()
    color: "#8A2BE2",
    width: "4",
    style: "dash"
  },
  polygonSymbol: {
    type: "simple-fill",  // autocasts as new SimpleMarkerSymbol()
    color: "rgba(138,43,226, 0.8)",
    style: "solid",
    outline: { // autocasts as new SimpleLineSymbol()
      color: "white",
      width: 1
    }
  }
});

Constructors

new SketchViewModel(properties)

Parameter:
properties Object
optional

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

Example:
// Typical usage
var sketch = new SketchViewModel({
  view: view
});

Property Overview

Any properties can be set, retrieved or listened to. See the Working with Properties topic.
NameTypeSummaryClass
String

The name of the class.

more details
more detailsAccessor
Graphic

Graphic representing the temporary geometry that is being drawn on the view.

more details
more detailsSketchViewModel
SimpleMarkerSymbol

A simple marker symbol used for drawing point geometries.

more details
more detailsSketchViewModel
SimpleFillSymbol

A simple fill symbol used for drawing polygon geometries.

more details
more detailsSketchViewModel
SimpleLineSymbol

A simple line symbol used for drawing polyline geometries.

more details
more detailsSketchViewModel
String

The view model's state.

more details
more detailsSketchViewModel
MapView

The view in which geometries will be sketched by the user.

more details
more detailsSketchViewModel

Property Details

declaredClassStringreadonly inherited

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

graphicGraphic

Graphic representing the temporary geometry that is being drawn on the view.

A simple marker symbol used for drawing point geometries.

polygonSymbolSimpleFillSymbol

A simple fill symbol used for drawing polygon geometries.

polylineSymbolSimpleLineSymbol

A simple line symbol used for drawing polyline geometries.

stateStringreadonly

The view model's state.

Known Values: ready | disabled

Default Value: disabled

The view in which geometries will be sketched by the user.

Method Overview

NameReturn TypeSummaryClass

Creates an instance of the requested draw action type.

more details
more detailsSketchViewModel

Emits an event on the instance.

more details
more detailsSketchViewModel
Boolean

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

more details
more detailsSketchViewModel
Object

Registers an event handler on the instance.

more details
more detailsSketchViewModel

Resets SketchViewModel to prepare for another create operation.

more details
more detailsSketchViewModel

Method Details

create(drawAction)

Creates an instance of the requested draw action type.

Known Values: point | polyline | polygon

Parameter:
drawAction String

Name of the draw action tool.

Example:
// create a new instance of sketch view model
// and associate it with a map view.
var sketch = new SketchViewModel({
  view: view
});

// set up the SketchViewModel to create polygon geometries
sketch.create("polygon");

emit(type, event)protected

Since: ArcGIS API for JavaScript 4.5

Emits an event on the instance. This method should only be used when creating subclasses that inherit from Evented.

Parameters:
type String

The name of the event.

event Object

The event payload.

hasEventListener(type){Boolean}

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:
TypeDescription
BooleanReturns true if the class supports the input event.

on(type, listener){Object}

Registers an event handler on the instance. Call this method to hook an event with a listener. See the Events summary table for a list of listened events.

Parameters:
type String

The name of event to listen for.

listener Function

The function to call when the event is fired.

Returns:
TypeDescription
ObjectReturns an event handler with a remove() method that can be called to stop listening for the event.
PropertyTypeDescription
removeFunctionWhen called, removes the listener from the event.
See also:
Example:
view.on("click", function(event){
  // event is the event handle returned after the event fires.
  console.log(event.mapPoint);
});

reset()

Resets SketchViewModel to prepare for another create operation.

Event Overview

NameTypeSummaryClass
{geometry: Geometry,graphic: Graphic}

Fires after user double clicks on the view or presses the C key to finish the sketch.

more details
more detailsSketchViewModel

Event Details

draw-complete

Fires after user double clicks on the view or presses the C key to finish the sketch.

Properties:
geometry Geometry

The geometry completed by the sketch view model.

graphic Graphic

The graphic presenting the completed geometry.

Example:
sketch.on("draw-complete", function (evt) {
  // get the graphic representing the completed
  // geometry and add it to the view.
  view.graphics.add(evt.graphic)
});

API Reference search results

NameTypeModule

There were no match results from your search criteria.