Skip to content
ESM
import "@arcgis/map-components/components/arcgis-distance-measurement-2d";
Inheritance:
ArcgisDistanceMeasurement2dPublicLitElement
Since
ArcGIS Maps SDK for JavaScript 4.28

The Distance Measurement 2D component can be added to an arcgis-map component to calculate and display the length of a polyline.

How distances are computed depends on the scene's spatial reference.

In geographic coordinate systems (GCS), and in Web Mercator, lengths are computed geodetically, taking into consideration the curvature of the planet.

In projected coordinate systems (PCS), apart from Web Mercator, lengths are computed in a Euclidean manner (in their respective PCS).

Demo

Properties

analysis

Property
Type
DistanceMeasurementAnalysis
Since
ArcGIS Maps SDK for JavaScript 5.0

The DistanceMeasurementAnalysis created or modified by the component.

When connecting the Distance Measurement 2D component to the arcgis-map component, it automatically creates an empty analysis and adds it to the Map's arcgis-map.analyses collection. You can then wait for the DistanceMeasurementAnalysisView2D to be created before accessing the analysis results.

// Get the Map component and the Distance Measurement 2D component, and wait until both are ready.
const viewElement = document.querySelector("arcgis-map");
await viewElement.viewOnReady();
const distanceMeasurement2dElement = document.querySelector("arcgis-distance-measurement-2d");
await distanceMeasurement2dElement.componentOnReady();
// Get the DistanceMeasurementAnalysis created by the Distance Measurement 2D component.
const analysis = distanceMeasurement2dElement.analysis;
// Get the DistanceMeasurementAnalysisView2D and watch for results.
const analysisView = await viewElement.whenAnalysisView(analysis);
const handle = reactiveUtils.watch(
() => analysisView?.result,
() => {
console.log("Analysis results:", analysisView.result);
},
);

Whenever the component is destroyed, the analysis is automatically removed from the collection.

Alternatively, a programmatically created DistanceMeasurementAnalysis can be provided to the component. Then, the application itself needs to add it to and later remove it from the analyses collection of the Map component.

// Create the DistanceMeasurementAnalysis.
const distanceMeasurementAnalysis = new DistanceMeasurementAnalysis({
geometry: new Polyline({
paths: [
[
[-118.25, 34.05], // Los Angeles
[-77.02, 38.90], // Washington, D.C.
]
],
}),
unit: "miles", // Display length in miles.
});
// Get the Map component and the Distance Measurement 2D component, and wait until both are ready.
const viewElement = document.querySelector("arcgis-map");
await viewElement.viewOnReady();
const distanceMeasurement2dElement = document.querySelector("arcgis-distance-measurement-2d");
await distanceMeasurement2dElement.componentOnReady();
// Add the analysis to the analyses collection of the Map component.
viewElement.analyses.add(distanceMeasurementAnalysis);
// Connect the analysis to the measurement component:
distanceMeasurement2dElement.analysis = distanceMeasurementAnalysis;

autoDestroyDisabled

Property
Type
boolean

If true, the component will not be destroyed automatically when it is disconnected from the document. This is useful when you want to move the component to a different place on the page, or temporarily hide it. If this is set, make sure to call the destroy() method when you are done to prevent memory leaks.

Attribute
auto-destroy-disabled
Default value
false

hideStartButton

Property
Type
boolean
Since
ArcGIS Maps SDK for JavaScript 5.0

If true, the button that starts a new measurement will be hidden.

Attribute
hide-start-button
Default value
false

hideUnitSelect

Property
Type
boolean
Since
ArcGIS Maps SDK for JavaScript 5.0

If true, the unit selection dropdown will be hidden.

Attribute
hide-unit-select
Default value
false

hideVisualization

Property
Type
boolean
Since
ArcGIS Maps SDK for JavaScript 5.0

Indicates whether the component's visualization is hidden in the view.

Attribute
hide-visualization
Default value
false

icon

reflected Property
Type
Icon["icon"] | undefined

Icon which represents the component. Typically used when the component is controlled by another component (e.g. by the Expand component).

See also
Attribute
icon
Default value
"measure-line"

label

Property
Type
string | undefined

The component's default label.

Attribute
label

referenceElement

Property
Type
ArcgisReferenceElement | string | undefined

By assigning the id attribute of the Map or Scene component to this property, you can position a child component anywhere in the DOM while still maintaining a connection to the Map or Scene.

See also
Attribute
reference-element

snappingOptions

Property
Type
SnappingOptions

The SnappingOptions for measuring.

state

readonly Property
Type
DistanceMeasurement2DState

The component's state. The values mean the following:

  • disabled - not ready yet
  • ready - ready for measuring
  • measuring - currently measuring
  • measured - measuring has finished

unit

Property
Type
SystemOrLengthUnit

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

Attribute
unit

unitOptions

Property
Type
Array<SystemOrLengthUnit>

List of available units and unit systems (imperial, metric) that are shown in the component's dropdown. 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.

view

Property
Type
MapView | undefined

The view associated with the component.

Note: The recommended approach is to fully migrate applications to use map and scene components and avoid using MapView and SceneView directly. However, if you are migrating a large application from widgets to components, you might prefer a more gradual transition. To support this use case, the SDK includes this view property which connects a component to a MapView or SceneView. Ultimately, once migration is complete, the arcgis-distance-measurement-2d component will be associated with a map or scene component rather than using the view property.

Methods

MethodSignature
clear(): Promise<void>
componentOnReady
inherited
componentOnReady(): Promise<this>
destroy(): Promise<void>
start(): Promise<void>

clear

Method
Signature
clear (): Promise<void>

Clears the current measurement.

Returns
Promise<void>

componentOnReady

inherited Method
Signature
componentOnReady (): Promise<this>
Inherited from: PublicLitElement

Creates a promise that resolves once the component is fully loaded.

Returns
Promise<this>
Example
const arcgisDistanceMeasurement2d = document.querySelector("arcgis-distance-measurement-2d");
document.body.append(arcgisDistanceMeasurement2d);
await arcgisDistanceMeasurement2d.componentOnReady();
console.log("arcgis-distance-measurement-2d is ready to go!");

destroy

Method
Signature
destroy (): Promise<void>

Permanently destroy the component.

Returns
Promise<void>

start

Method
Signature
start (): Promise<void>

Starts a new measurement.

Returns
Promise<void>

Events

NameType
CustomEvent<{ name: "analysis" | "state"; }>

arcgisPropertyChange

Event
arcgisPropertyChange: CustomEvent<{ name: "analysis" | "state"; }>

Emitted when the value of a property is changed. Use this to listen to changes to properties.

bubbles composed cancelable

arcgisReady

Event
arcgisReady: CustomEvent<void>

Emitted when the component associated with a map or scene view is ready to be interacted with.

bubbles composed cancelable