Skip to main content
Version: 1.8.3.X

Feature Drawing Events

Feature drawing events let you respond when a user draws, edits, cancels or deletes a feature within a shape. For changes to the whole shape, see Shape Events. Map filters have their own drawing events.

The examples use the control obtained in Registering for Map Events. The parameters are passed to your function in the order shown. shapeId identifies the shape containing the feature, not the feature itself.

FeatureDrawingStarted​

The FeatureDrawingStarted event is triggered when a user selects a drawing tool or starts editing a feature's geometry.

Parameters:

  • shapeName (string): The name of the shape containing the feature.
  • featureType (string): The drawing tool or geometry type, such as 'Point', 'LineString', 'Polygon', 'Circle' or 'Extent'.
  • startLat (number or undefined): undefined when the tool opens, before the user has entered coordinates.
  • startLng (number or undefined): undefined for the same reason.
  • shapeId (string): The ID of the shape containing the feature.

Opening Plot Point or Text reports 'Point'. Placing the text afterwards does not trigger a second Started event.

Usage:

control.on('FeatureDrawingStarted', (shapeName, featureType, startLat, startLng, shapeId) => {
console.log('Activated', shapeId, shapeName, featureType);
console.log(startLat, startLng); // undefined, undefined
});

FeatureDrawingCompleted​

The FeatureDrawingCompleted event is triggered when a user finishes a valid drawing or confirms a geometry edit. It provides the completed geometry and the name and ID of the shape containing it. Plot Point confirmation also triggers this event.

Parameters:

  • shapeName (string): The name of the shape containing the feature.
  • featureType (string): The drawing tool or edited geometry type.
  • finishLat (number): The latitude of the geometry's last coordinate.
  • finishLong (number): The longitude of the geometry's last coordinate.
  • geometry (string): The completed GeoJSON Geometry as a JSON string.
  • shapeId (string): The ID of the shape containing the feature.

Use JSON.parse(geometry) to read the geometry. It does not contain feature properties or a feature ID; use the FeatureCollection from ShapeDrawingModified/Completed if you need those values.

The finish coordinates describe the result, not necessarily the last pointer position. A closed polygon repeats its first coordinate at the end. Circle and Extent tools produce Polygon geometry while featureType remains 'Circle' or 'Extent'.

The shape's Started/Modified/Completed events occur first. Transform and property/title/style changes do not trigger FeatureDrawingCompleted; use ShapeDrawingModified for those. A failed operation, or one whose shape or feature is no longer available, may not trigger completion.

Usage:

control.on('FeatureDrawingCompleted', (shapeName, featureType, finishLat, finishLong, geometry, shapeId) => {
const drawnGeometry = JSON.parse(geometry);
console.log(shapeId, shapeName, featureType, finishLat, finishLong, drawnGeometry);
});

For example, the fifth argument can be the string '{"type":"Point","coordinates":[120,-30]}'; the finish arguments are then -30, 120. GeoJSON coordinate arrays use longitude, latitude, unlike the callback's latitude-first arguments.

FeatureDrawingCancelled​

The FeatureDrawingCancelled event is triggered when a user cancels drawing or editing a feature, including closing Plot Point or Text without completing it.

Parameters:

  • shapeName (string): The name of the shape containing the feature.
  • shapeId (string): The ID of that shape.

Cancelling discards a new unfinished feature or restores the geometry being edited. It does not delete the shape or undo earlier property changes. Transform Cancel does not trigger this event, and ShapeDrawingCancelled is no longer triggered.

Usage:

control.on('FeatureDrawingCancelled', (shapeName, shapeId) => {
console.log('Drawing/edit cancelled', shapeId, shapeName);
});

FeatureDrawingDeleted​

The FeatureDrawingDeleted event is triggered when a user deletes one or more features from a shape.

Parameters:

  • shapeName (string): The name of the shape containing the deleted features.
  • featureType (string): The geometry type for a single feature, or 'GeometryCollection' when multiple features are deleted together.
  • shapeId (string): The ID of that shape.

This event occurs before the shape's update events. It does not include deleted-feature IDs or geometry. Use ShapeDrawingCleared/Deleted for clearing or deleting the whole shape; those actions do not trigger a separate deletion event for every feature.

Usage:

control.on('FeatureDrawingDeleted', (shapeName, featureType, shapeId) => {
console.log('Removed feature(s)', shapeId, shapeName, featureType);
});

Removing listeners​

control.off('FeatureDrawingStarted');
control.off('FeatureDrawingCompleted');
control.off('FeatureDrawingCancelled');
control.off('FeatureDrawingDeleted');

These calls remove all listeners for each named event. Use control.off(eventName, callback) with the original function reference to remove only your own listener.