This sample demonstrates measuring 3D distances between two points in a scene.
The distance measurement analysis allows you to add the same measuring experience found in ArcGIS Pro, City Engine, and the ArcGIS API for JavaScript to your app. You can set the unit system of measurement (metric or imperial) and have the units automatically switch to one appropriate for the current scale. The rendering is handled internally so they do not interfere with other analyses like viewsheds.
How to use the sample
Choose a unit system for the measurement in the UI dropdown. Click any location in the scene to set the starting measuring point. Press, hold, and drag to a location to update the end location.
How it works
To measure distances with the LocationDistanceMeasurement analysis:
Create an AnalysisOverlay and add it to your scene view's analysis overlay collection.
Create a LocationDistanceMeasurement, specifying the startLocation and endLocation. To start with, these locations can be the same. Add the analysis to the analysis overlay. The measuring line will be drawn between the two points.
The directDistanceChanged, verticalDistanceChanged, and horizontalDistanceChanged signals will emit when the distances change, giving access to the new values for the directDistance, horizontalDistance, and verticalDistance. The distance objects contain both the scalar value and unit of measurement.
Relevant API
AnalysisOverlay
LocationDistanceMeasurement
Additional information
The LocationDistanceMeasurement analysis only performs planar distance calculations. This may not be appropriate for large distances where the Earth's curvature needs to be taken into account.
Tags
3D, Analysis
Sample Code
DistanceMeasurementAnalysis.qml
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// [WriteFile Name=DistanceMeasurementAnalysis, Category=Analysis]// [Legal]// Copyright 2018 Esri.// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]import QtQuick
import QtQuick.Controls
import Esri.ArcGISRuntime
import Esri.ArcGISExtras
Rectangle {
id: rootRectangleclip: truewidth: 800height: 600property bool isNavigating: falseSceneView {
id: sceneViewanchors.fill: parentComponent.onCompleted: {
// Set the focus on SceneView to initially enable keyboard navigation forceActiveFocus();
}
property bool isPressAndHeld: false// Declare a SceneScene {
id: scene// Set the basemapBasemap {
initStyle: Enums.BasemapStyleArcGISTopographic
}
// Add a Scene LayerArcGISSceneLayer {
url: "https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Buildings_Brest/SceneServer/layers/0"altitudeOffset: 1 }
// Set the SurfaceSurface {
ArcGISTiledElevationSource {
url: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer" }
}
// set initial viewpointViewpointCenter {
center: locationDistanceMeasurement.startLocation
targetScale: 200Camera {
location: locationDistanceMeasurement.startLocation
distance: 400pitch: 45heading: 0roll: 0 }
}
}
// Declare an AnalysisOverlayAnalysisOverlay {
id: analysisOverlay// Declare a Location Distance MeasurementLocationDistanceMeasurement {
id: locationDistanceMeasurementproperty string unitLabel: unitSystem === Enums.UnitSystemMetric ? "m" : "ft"// set unit systemunitSystem: Enums.UnitSystemMetric
// set the start pointstartLocation: Point {
x: -4.494677y: 48.384472z: 24.772694spatialReference: SpatialReference { wkid: 4326 }
}
// set the end pointendLocation: Point {
x: -4.495646y: 48.384377z: 58.501115spatialReference: SpatialReference { wkid: 4326 }
}
// connect to distance change signalsonDirectDistanceChanged: directDistanceText.text = directDistance.value.toFixed(2) + " %1".arg(directDistance.unit.abbreviation)
onHorizontalDistanceChanged: horizontalDistanceText.text = horizontalDistance.value.toFixed(2) + " %1".arg(horizontalDistance.unit.abbreviation)
onVerticalDistanceChanged: verticalDistanceText.text = verticalDistance.value.toFixed(2) + " %1".arg(verticalDistance.unit.abbreviation)
}
}
// handle mouse signals to update the analysis// When the mouse is pressed and held, start updating the distance analysis end pointonMousePressedAndHeld: mouse => {
isPressAndHeld = true;
sceneView.screenToLocation(mouse.x, mouse.y);
}
// When the mouse is released...onMouseReleased: mouse => {
// Check if the mouse was released from a pan gestureif (isNavigating) {
isNavigating = false;
return;
}
// Ignore if Right clickif (mouse.button === Qt.RightButton)
return;
// If pressing and holding, do nothingif (isPressAndHeld)
isPressAndHeld = false;
// Else get the location from the screen coordinateselse sceneView.screenToLocation(mouse.x, mouse.y);
}
// Set a flag when mousePressed signal emitsonMousePressed: {
isNavigating = false;
}
// Update the distance analysis when the mouse moves if it is a press and hold movementonMousePositionChanged: mouse => {
if (isPressAndHeld)
sceneView.screenToLocation(mouse.x, mouse.y);
}
// When screenToLocation completes...onScreenToLocationCompleted: location => {
if (isPressAndHeld)
locationDistanceMeasurement.endLocation = location;
else locationDistanceMeasurement.startLocation = location;
}
// Set a flag when viewpointChanged signal emitsonViewpointChanged: {
isNavigating = true;
}
}
Rectangle {
anchors {
fill: resultsColumn
margins: -5 }
color: "black"opacity: 0.5radius: 5 }
Column {
id: resultsColumnanchors {
left: parent.left
top: parent.top
margins: 10 }
spacing: 5Row {
spacing: 5Text {
text: "Direct Distance:"color: "white" }
Text {
id: directDistanceTextcolor: "white" }
}
Row {
spacing: 5Text {
text: "Vertical Distance:"color: "white" }
Text {
id: verticalDistanceTextcolor: "white" }
}
Row {
spacing: 5Text {
text: "Horizontal Distance:"color: "white" }
Text {
id: horizontalDistanceTextcolor: "white" }
}
Row {
spacing: 5Text {
text: "Unit System:"color: "white" }
ComboBox {
id: comboBoxmodel: ["Metric", "Imperial"]
onCurrentTextChanged: {
if (currentText === "Metric")
locationDistanceMeasurement.unitSystem = Enums.UnitSystemMetric;
else locationDistanceMeasurement.unitSystem = Enums.UnitSystemImperial;
}
}
}
}
}