Skip to content

DrawTool

DrawTool is maptalks' core drawing tool. It extends MapTool. It lets users draw point, line and polygon features by clicking on the map, and serves as the foundation for higher-level tools such as distance measurement and area measurement. After a drawing finishes, the drawend event fires and the drawn geometry is returned.

js
import { DrawTool } from "maptalks";

const drawTool = new DrawTool({
  mode: "Polygon",
  symbol: {
    lineColor: "#1bc8f8"
  }
}).addTo(map);

Constructor

js
new DrawTool(options)

Parameters:

  • options — DrawTool options. Most properties match the DrawTool.options table below.

options

NameTypeDescriptionDefault
modeStringThe drawing mode name, corresponding to a registered modenull
symbolObjectThe symbol style of the drawingnull
onceBooleanWhether to disable the tool automatically after drawing finishesfalse
autoPanAtEdgeBooleanWhether to auto-pan the map when reaching the edgefalse
blockGeometryEventsBooleanWhether to block geometry events while drawingfalse
zIndexNumberThe layer z-index of the toolNumber.MAX_VALUE
doubleClickZoomBooleanWhether double-click zooming is allowed while drawingfalse
enableAltitudeBooleanWhether altitude is enabledtrue
interactiveBooleanWhether the tool responds to interactiontrue

Member Methods

  • getMode(): String — Gets the current drawing mode.
  • setMode(mode): DrawTool — Sets the drawing mode.
  • getSymbol(): Object — Gets the current drawing symbol.
  • setSymbol(symbol): DrawTool — Sets the drawing symbol.
  • getCurrentGeometry(): Geometry — Gets the geometry currently being drawn.
  • undo() — Undoes the last step.
  • redo() — Redoes the last step.
  • endDraw(param): DrawTool — Ends the current drawing, param is the ending parameter.
  • addCoordinate(coordinate): DrawTool — Adds a coordinate to the current drawing.
  • setLayerZIndex(z): DrawTool — Sets the z-index of the tool's layer.

Static Methods

  • registerMode(name, modeAction) — Registers a custom drawing mode.
  • getRegisterMode(name): Object — Gets a registered drawing mode.
  • getAllRegisterMode(): Object — Gets all registered drawing modes.

Events

  • drawprepare — Fired before drawing starts.
  • drawstart — Fired when drawing starts.
  • drawvertex — Fired when a new vertex is added while drawing.
  • drawend — Fired when drawing finishes.
  • mousemove — Fired when the mouse moves while drawing.