ArcGIS Runtime SDK for .NET Samples

Distance measurement analysis

Measure distance in a scene view


// 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:
// 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.

using System;
using System.Diagnostics;
using CoreGraphics;
using Esri.ArcGISRuntime;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI.Controls;
using Esri.ArcGISRuntime.UI.GeoAnalysis;
using Foundation;
using UIKit;

namespace ArcGISRuntime.Samples.DistanceMeasurement
        "Distance measurement analysis",
        "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.",
        "Tap to set a new end point for the measurement.",
    public class DistanceMeasurement : UIViewController
        // Create and hold references to the UI controls.
        private readonly UIToolbar _helpToolbar = new UIToolbar();
        private SceneView _mySceneView;
        private UILabel _helpLabel;
        private UILabel _resultLabel;
        private UIToolbar _resultArea;
        private UIButton _unitChangeButton;

        // URLs to various services used to provide an interesting scene for the sample.
        private readonly Uri _buildingService = new Uri("");
        private readonly Uri _worldElevationService = new Uri("");

        // Reference to the measurement.
        private LocationDistanceMeasurement _distanceMeasurement;

        public DistanceMeasurement()
            Title = "Distance measurement analysis";

        private void Initialize()
            // Create a scene with elevation.
            var sceneSurface = new Surface();
            sceneSurface.ElevationSources.Add(new ArcGISTiledElevationSource(_worldElevationService));
            var myScene = new Scene(Basemap.CreateImagery())
                BaseSurface = sceneSurface

            // Create and add a building layer.
            var buildingsLayer = new ArcGISSceneLayer(_buildingService);

            // Create and add an analysis overlay.
            var measureAnalysisOverlay = new AnalysisOverlay();

            // Create an initial distance measurement and show it.
            var start = new MapPoint(-4.494677, 48.384472, 24.772694, SpatialReferences.Wgs84);
            var end = new MapPoint(-4.495646, 48.384377, 58.501115, SpatialReferences.Wgs84);
            _distanceMeasurement = new LocationDistanceMeasurement(start, end);
            _mySceneView.SetViewpointCamera(new Camera(start, 200, 45, 45, 0));

            // Keep the UI updated.
            _distanceMeasurement.MeasurementChanged += (o, e) =>
                // This is needed because measurement change events occur on a non-UI thread and this code accesses UI object.
                BeginInvokeOnMainThread(() =>
                    // Update the labels with new values in the format {value} {unit system}.
                    string direct =
                        $"{_distanceMeasurement.DirectDistance.Value:F} {_distanceMeasurement.DirectDistance.Unit.Abbreviation}";
                    string vertical =
                        $"{_distanceMeasurement.VerticalDistance.Value:F} {_distanceMeasurement.VerticalDistance.Unit.Abbreviation}";
                    string horizontal =
                        $"{_distanceMeasurement.HorizontalDistance.Value:F} {_distanceMeasurement.HorizontalDistance.Unit.Abbreviation}";
                    _resultLabel.Text = $"Direct: {direct}, V: {vertical}, H: {horizontal}";

            // Show the scene in the view.
            _mySceneView.Scene = myScene;

            // Subscribe to tap events to enable updating the measurement.
            _mySceneView.GeoViewTapped += MySceneView_GeoViewTapped;

        private async void MySceneView_GeoViewTapped(object sender, GeoViewInputEventArgs e)
                // Get the geographic location for the current mouse position.
                MapPoint geoPoint = await _mySceneView.ScreenToLocationAsync(e.Position);

                if (geoPoint == null) return;

                // Update the location distance measurement.
                _distanceMeasurement.EndLocation = geoPoint;
            catch (Exception ex)

        private void CreateLayout()
            // Create the scene view.
            _mySceneView = new SceneView();

            // Create the help label.
            _helpLabel = new UILabel
                Text = "Tap to update.",
                TextAlignment = UITextAlignment.Center

            // Create the result label.
            _resultLabel = new UILabel
                TextAlignment = UITextAlignment.Center,
                AdjustsFontSizeToFitWidth = true

            // Create the result toolbar.
            _resultArea = new UIToolbar();

            // Create the unit change button.
            _unitChangeButton = new UIButton();
            _unitChangeButton.SetTitle("Unit systems", UIControlState.Normal);
            _unitChangeButton.BackgroundColor = View.TintColor;
            _unitChangeButton.SetTitleColor(UIColor.White, UIControlState.Normal);
            _unitChangeButton.Layer.CornerRadius = 10;
            _unitChangeButton.ClipsToBounds = true;

            // Handle the unit change button press.
            _unitChangeButton.TouchUpInside += UnitChangeButton_TouchUpInside;

            // Add views to the page.
            View.AddSubviews(_mySceneView, _helpToolbar, _resultArea, _helpLabel, _resultLabel, _unitChangeButton);

        private void UnitChangeButton_TouchUpInside(object sender, EventArgs e)
            // Create the view controller that will present the list of unit systems.
            UIAlertController unitSystemSelectionAlert =
                UIAlertController.Create("Change unit system", "", UIAlertControllerStyle.ActionSheet);

            // Needed to prevent a crash on iPad.
                presentationPopover = unitSystemSelectionAlert.PopoverPresentationController;
            if (presentationPopover != null)
                presentationPopover.SourceView = View;
                presentationPopover.PermittedArrowDirections = UIPopoverArrowDirection.Up;

            // Show an option for each unit system.
            foreach (UnitSystem system in Enum.GetValues(typeof(UnitSystem)))
                // Upon selecting a unit system, update the distance measure.
                unitSystemSelectionAlert.AddAction(UIAlertAction.Create(system.ToString(), UIAlertActionStyle.Default,
                    action => _distanceMeasurement.UnitSystem = system));

            // Show the alert.
            PresentViewController(unitSystemSelectionAlert, true, null);

        public override void ViewDidLoad()


        public override void ViewDidLayoutSubviews()
                nfloat topMargin = NavigationController.NavigationBar.Frame.Height + UIApplication.SharedApplication.StatusBarFrame.Height;
                nfloat toolbarHeight = 40;
                nfloat controlHeight = 30;

                // Reposition the views.
                _mySceneView.Frame = new CGRect(0, 0, View.Bounds.Width, View.Bounds.Height);
                _mySceneView.ViewInsets = new UIEdgeInsets(topMargin + toolbarHeight, 0, toolbarHeight, 0);
                _helpToolbar.Frame = new CGRect(0, topMargin, View.Bounds.Width, 40);
                _helpLabel.Frame = new CGRect(5, topMargin + 5, View.Bounds.Width - 10, controlHeight);
                _resultArea.Frame = new CGRect(0, View.Bounds.Height - toolbarHeight, View.Bounds.Width, toolbarHeight);
                _resultLabel.Frame = new CGRect(10, View.Bounds.Height - toolbarHeight + 5, View.Bounds.Width - 20, toolbarHeight - 10);
                _unitChangeButton.Frame = new CGRect(View.Bounds.Width / 4, View.Bounds.Height - 3 * toolbarHeight, View.Bounds.Width / 2, toolbarHeight);

            // Needed to prevent crash when NavigationController is null. This happens sometimes when switching between samples.
            catch (NullReferenceException)

In this topic
  1. Code