Distance measurement analysis

View on GitHub
Sample viewer app

Measure distances between two points in 3D.

Image of distance measurement analysis

Use case

The distance measurement analysis allows you to add to your app the same interactive measuring experience found in ArcGIS Pro, City Engine, and the ArcGIS API for JavaScript. You can set the unit system of measurement (metric or imperial). The units automatically switch to one appropriate for the current scale.

How to use the sample

Choose a unit system for the measurement in the UI dropdown. Single tap any location on the screen to set the start point of measurement. Double tap and drag to set the end point of measurement. Single tapping any new location will start a new measurement.

How it works

  1. Create an AnalysisOverlay object and add it to the analysis overlay collection of the SceneView object.
  2. Specify the start location and end location to create a LocationDistanceMeasurement object. Initially, the start and end locations can be the same point.
  3. Add the location distance measurement analysis to the analysis overlay.
  4. The measurementChanged callback will trigger if the distances change. You can get the new values for the directDistance, horizontalDistance, and verticalDistance from the measurementChangedEvent object returned by the callback.

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 must be considered.

Tags

3D, analysis, distance, measure

Sample Code

MainActivity.java
                                                                                                                                                                                                                                    
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
/* 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.
 *
 */

package com.esri.arcgisruntime.sample.distancemeasurementanalysis;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.esri.arcgisruntime.UnitSystem;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geoanalysis.LocationDistanceMeasurement;
import com.esri.arcgisruntime.geometry.Distance;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.layers.ArcGISSceneLayer;
import com.esri.arcgisruntime.mapping.ArcGISScene;
import com.esri.arcgisruntime.mapping.ArcGISTiledElevationSource;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.Surface;
import com.esri.arcgisruntime.mapping.view.AnalysisOverlay;
import com.esri.arcgisruntime.mapping.view.Camera;
import com.esri.arcgisruntime.mapping.view.DefaultSceneViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.SceneView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {

  private SceneView mSceneView;

  private TextView mDirectDistance;

  private TextView mVerticalDistance;

  private TextView mHorizontalDistance;

  private LocationDistanceMeasurement distanceMeasurement;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // create a scene and add a basemap to it
    final ArcGISScene scene = new ArcGISScene();
    scene.setBasemap(Basemap.createTopographic());

    // inflate views from layout
    mSceneView = findViewById(R.id.sceneView);
    mDirectDistance = findViewById(R.id.totalDistance);
    mHorizontalDistance = findViewById(R.id.horizontalDistance);
    mVerticalDistance = findViewById(R.id.verticalDistance);
    Spinner unitSpinner = findViewById(R.id.units_spinner);
    // set the scene to the view
    mSceneView.setScene(scene);

    // add base surface for elevation data
    Surface surface = new Surface();
    surface.getElevationSources().add(new ArcGISTiledElevationSource(
        "https://scene.arcgis.com/arcgis/rest/services/BREST_DTM_1M/ImageServer"));
    scene.setBaseSurface(surface);

    // add building layer
    ArcGISSceneLayer sceneLayer = new ArcGISSceneLayer(
        "https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Buildings_Brest/SceneServer/layers/0");
    // offset for visual purposes
    sceneLayer.setAltitudeOffset(1);
    scene.getOperationalLayers().add(sceneLayer);

    // create analysis overlay and add it to scene
    AnalysisOverlay analysisOverlay = new AnalysisOverlay();
    mSceneView.getAnalysisOverlays().add(analysisOverlay);

    // initialize a distance measurement and add it to the analysis overlay
    Point start = new Point(-4.494677, 48.384472, 24.772694, SpatialReferences.getWgs84());
    Point end = new Point(-4.495646, 48.384377, 58.501115, SpatialReferences.getWgs84());
    distanceMeasurement = new LocationDistanceMeasurement(start, end);
    analysisOverlay.getAnalyses().add(distanceMeasurement);

    // zoom to initial measurement
    Camera camera = new Camera(start, 300.0, 315.0, 60.0, 0.0);
    mSceneView.setViewpointCamera(camera);

    // initialize a list to contain the available units
    ArrayList<String> unitsList = new ArrayList<>();
    for (UnitSystem unitSystemItem : UnitSystem.values()) {
      unitsList.add(unitSystemItem.toString());
    }

    // set up drop-down list
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,
        unitsList);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    unitSpinner.setAdapter(adapter);
    unitSpinner.setSelection(1);

    unitSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override public void onItemSelected(AdapterView<?> adapterView, View view, int position,
          long l) {
        switch (position) {
          case 0:
            distanceMeasurement.setUnitSystem(UnitSystem.IMPERIAL);
            break;
          case 1:
            distanceMeasurement.setUnitSystem(UnitSystem.METRIC);
            break;
          default:
            Toast.makeText(MainActivity.this, "Unsupported option", Toast.LENGTH_SHORT).show();
            break;
        }
      }

      @Override public void onNothingSelected(AdapterView<?> adapterView) {
      }
    });

    // show the distances in the UI when the measurement changes
    final DecimalFormat decimalFormat = new DecimalFormat("#.##");
    distanceMeasurement.addMeasurementChangedListener(measurementChangedEvent -> {
      Distance directDistance = distanceMeasurement.getDirectDistance();
      Distance verticalDistance = distanceMeasurement.getVerticalDistance();
      Distance horizontalDistance = distanceMeasurement.getHorizontalDistance();

      mDirectDistance.setText(String.format("%s %s",
          decimalFormat.format(directDistance.getValue()),
          directDistance.getUnit().getAbbreviation()));
      mHorizontalDistance.setText(String.format("%s %s",
          decimalFormat.format(horizontalDistance.getValue()),
          horizontalDistance.getUnit().getAbbreviation()));
      mVerticalDistance.setText(String.format("%s %s",
          decimalFormat.format(verticalDistance.getValue()),
          verticalDistance.getUnit().getAbbreviation()));
    });

    // add onTouchListener to set the start point and end point with a SingleTap and a DoubleTapDrag
    mSceneView.setOnTouchListener(new DefaultSceneViewOnTouchListener(mSceneView) {
      @Override
      public boolean onSingleTapConfirmed(MotionEvent motionEvent) {

        // convert from screen point to location point
        android.graphics.Point screenPoint = new android.graphics.Point(
            Math.round(motionEvent.getX()),
            Math.round(motionEvent.getY()));
        ListenableFuture<Point> locationPointFuture = mSceneView.screenToLocationAsync(screenPoint);
        locationPointFuture.addDoneListener(() -> {

          try {
            Point location = locationPointFuture.get();
            distanceMeasurement.setStartLocation(location);

          } catch (InterruptedException | ExecutionException e) {
            String error = "Error converting screen point to location point ";
            Log.e(MainActivity.this.toString(), error + ":" + e.getMessage());
            Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
          }
        });
        return true;
      }

      @Override
      public boolean onDoubleTouchDrag(MotionEvent motionEvent) {

        // convert from screen point to location point
        android.graphics.Point screenPoint = new android.graphics.Point(
            Math.round(motionEvent.getX()),
            Math.round(motionEvent.getY()));
        ListenableFuture<Point> locationPointFuture = mSceneView.screenToLocationAsync(screenPoint);
        locationPointFuture.addDoneListener(() -> {

          try {
            Point location = locationPointFuture.get();
            distanceMeasurement.setEndLocation(location);

          } catch (InterruptedException | ExecutionException e) {
            String error = "Error converting screen point to location point ";
            Log.e(MainActivity.this.toString(), error + ":" + e.getMessage());
            Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
          }
        });
        return true;
      }
    });
  }

  @Override
  protected void onPause() {
    super.onPause();
    // pause SceneView
    mSceneView.pause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    // resume SceneView
    mSceneView.resume();
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    // dispose SceneView
    mSceneView.dispose();
  }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.