Skip to content
import DistanceMeasurement2DViewModel from "@arcgis/core/widgets/DistanceMeasurement2D/DistanceMeasurement2DViewModel.js";
Inheritance:
DistanceMeasurement2DViewModelAccessor
Since
ArcGIS Maps SDK for JavaScript 4.10

Provides the logic for the DistanceMeasurement2D widget and component.

See also

Constructors

Constructor

Constructor
Parameters
ParameterTypeDescriptionRequired
properties
See the properties table for a list of all the properties that may be passed into the constructor.

Properties

Any properties can be set, retrieved or listened to. See the Watch for changes topic.

declaredClass

readonlyinherited Property
Type
string
Inherited from: Accessor

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

measurement

readonly Property
Type
LinearMeasurement | null | undefined

The length and geometry of the measurement polyline in meters.

Example
// After creating and adding the DistanceMeasurement2D widget
let measurementWidget = new DistanceMeasurement2D({
view: view
});
view.ui.add(measurementWidget, "top-right");
// Raw measurements (in meters) can be accessed from this property
reactiveUtils.watch(
() => measurementWidget.viewModel.measurement,
(measurement) => {
console.log(
"Length: ", measurement.length,
"Geometry: ", measurement.geometry
);
}
);

measurementLabel

readonly Property
Type
string | null | undefined

This property returns the locale specific representation of the length. Lengths are rounded to two decimal places. Lengths are sourced from the measurement property (in meters) and converted to the user defined units or system.

Example
// After creating and adding the DistanceMeasurement2D widget
let measurementWidget = new DistanceMeasurement2D({
view: view
});
view.ui.add(measurementWidget, "top-right");
// The measurement label can be accessed from this property
reactiveUtils.watch(
() => measurementWidget.viewModel.measurementLabel,
(label) => console.log("Label: ", label)
);

snappingOptions

autocast Property
Type
SnappingOptions
Since
ArcGIS Maps SDK for JavaScript 4.32
beta

The SnappingOptions for sketching. It supports self and feature snapping.

state

readonly Property
Type
MeasurementState

The view model's state.

ValueDescription
disablednot ready yet
readyready for measuring
measuringmeasuring has started
measuredmeasuring has finished
Default value
"disabled"
Example
// To display the state of the DistanceMeasurement2D widget
let measurementWidget = new DistanceMeasurement2D({ view });
reactiveUtils.watch(
() => measurementWidget.viewModel.state,
(state) => console.log("Current state: ", state)
);

unit

autocast Property
Type
SystemOrLengthUnit

Unit system (imperial, metric) or specific unit used for displaying the distance values. Possible values are listed in unitOptions.

Example
// To create the DistanceMeasurement2D widget that displays distance in yards
let measurementWidget = new DistanceMeasurement2D({
viewModel: {
view: view,
unit: "yards"
}
});
// To display the current measurement unit
console.log('Current unit: ', measurementWidget.viewModel.unit);

unitOptions

autocast Property
Type
SystemOrLengthUnit[]

List of available units and unit systems (imperial, metric) for displaying the distance values. By default, the following units are included: metric, imperial, inches, feet, us-feet, yards, miles, nautical-miles, meters, kilometers. Possible unit values can only be a subset of this list.

Example
// To display the available units to the console
let measurementWidget = new DistanceMeasurement2D({
view: view
});
console.log('All units: ', measurementWidget.viewModel.unitOptions.join(", "));

view

Property
Type
MapView | null | undefined

The view from which the widget will operate.

Example
// To create DistanceMeasurement2D widget with the view property
let measurementWidget = new DistanceMeasurement2D({
viewModel: {
view: view
}
});

Methods

MethodSignatureClass
clear(): void
start(): Promise<void>

clear

Method
Signature
clear (): void
Since
ArcGIS Maps SDK for JavaScript 4.16

Clears the current measurement.

Returns
void

start

Method
Signature
start (): Promise<void>
Since
ArcGIS Maps SDK for JavaScript 4.16

Starts a new measurement.

Returns
Promise<void>
Example
const distanceMeasurement2DViewModel = new DistanceMeasurement2DViewModel({
view: view,
unit: "us-feet"
});
await distanceMeasurement2DViewModel.start();