Measurement Events
Measurement events let you respond when a user measures distance or area on a dashboard or record-level map, including when they edit an existing measurement.
The examples use the control obtained in Registering for Map Events. The parameters are passed to your function in the order shown. These events do not include a measurement ID.
MeasurementStarted
The MeasurementStarted event is triggered when a user opens a measurement tool or starts editing a measurement.
Parameters:
measurementType(string):'Length'for distance or'Area'for area.startLat(number or undefined):undefinedwhen the tool opens, before coordinates are entered.startLng(number or undefined):undefinedfor the same reason.
Usage:
control.on('MeasurementStarted', (measurementType, startLat, startLng) => {
console.log('Activated measurement', measurementType);
console.log(startLat, startLng); // undefined, undefined
});
MeasurementCompleted
The MeasurementCompleted event is triggered when a user completes a valid measurement or confirms an edit. It provides the measured geometry, finish coordinates and measurement result as display text.
Parameters:
measurementType(string):'Length'or'Area'.finishLat(number): The latitude of the geometry's last coordinate.finishLong(number): The longitude of the geometry's last coordinate.polygon(string): The measured GeoJSON Geometry as a JSON string.measurementText(string): The displayed result, including units and formatting. This can be empty.
Despite the name polygon, a distance measurement normally contains LineString geometry; an area measurement contains Polygon geometry. Use JSON.parse(polygon) to read it. It is not a FeatureCollection.
The finish coordinates come from the resulting geometry, not necessarily the last pointer position. For a closed polygon, the final coordinate repeats its first vertex. Completion is not triggered if a valid geometry, coordinate or calculated label is unavailable.
Usage:
control.on('MeasurementCompleted', (measurementType, finishLat, finishLong, polygon, measurementText) => {
const measuredGeometry = JSON.parse(polygon);
console.log(measurementType, finishLat, finishLong, measuredGeometry, measurementText);
});
For example, polygon can be the string '{"type":"LineString","coordinates":[[120,-30],[121,-31]]}', with finish arguments -31, 121. GeoJSON arrays use longitude-first ordering.
MeasurementCancelled
The MeasurementCancelled event is triggered when a user cancels a measurement.
Parameters:
measurementType(string): The type of measurement cancelled:'Length'or'Area'.
An unfinished new measurement is discarded. Cancelling an edit restores the original measurement and label without triggering MeasurementCompleted. Other measurements are not cleared.
Usage:
control.on('MeasurementCancelled', (measurementType) => {
console.log('Measurement cancelled', measurementType);
});
MeasurementsCleared
The MeasurementsCleared event is triggered when a user clears all measurements. It occurs after measurement geometry, labels and detailed segments are removed, even if there were no measurements to remove.
Parameters: None.
Deleting a single measurement does not trigger this event, and there is no separate public MeasurementDeleted event. Use MeasurementCancelled to respond to cancelling a measurement tool.
Usage:
control.on('MeasurementsCleared', () => {
console.log('All measurements cleared');
});
Removing listeners
control.off('MeasurementStarted');
control.off('MeasurementCompleted');
control.off('MeasurementCancelled');
control.off('MeasurementsCleared');
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.