Measure a distance in 3D

Learn how to evaluate the horizontal, vertical, and direct distances between two points in a 3D Scene.

measure a distance in 3d

A distance measurement analysis is a type of measurement analysis that calculates and displays the distance between start point and end point locations. The analysis evaluates the vertical, horizontal, and direct distances between the two 3D points and renders a measurement visualization on-screen.

In this tutorial you will perform and display a distance measurement analysis in a web scene. Using the distance measurement analysis you will measure distances between hotspots in the Yosemite Valley.

Prerequisites

The following are required for this tutorial:

  1. An ArcGIS account to access your API keys. If you don't have an account, sign up for free.
  2. Your system meets the system requirements.
  3. The ArcGIS Runtime API for iOS is installed.

Steps

Open an Xcode project

This tutorial uses the completed Display a web scene tutorial as a starting point. You can first complete that tutorial or download and unzip the completed solution.

  1. To start this tutorial, first complete the Display a web scene tutorial or download and unzip the solution.
  2. Open the .xcodeproj file in Xcode.

Set the API Key

An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.

  1. Go to your developer dashboard to get your API key. For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.

  2. In Xcode, in the Project Navigator, click AppDelegate.swift.

  3. In the editor, set the apiKey property on the AGSArcGISRuntimeEnvironment with your API key.


    AppDelegate.swift
    Change line
                                       
    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
    // Copyright 2021 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
    //
    //   https://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.
    
    
    import UIKit
    
    import ArcGIS
    
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
        var window: UIWindow?
    
        func application(_ application: UIApplication,
                         didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // Note: it is not best practice to store API keys in source code.
            // The API key is referenced here for the convenience of this tutorial.
    
            AGSArcGISRuntimeEnvironment.apiKey = "YOUR_API_KEY"
    
            return true
        }
    
    }
    

Get the web scene item ID

You can use ArcGIS tools to create and view web scenes. Use the Scene Viewer to identify the web scene item ID. This item ID will be used later in the tutorial.

  1. Go to the Yosemite Valley Hotspots web scene in the Scene Viewer in ArcGIS Online. This web scene displays terrain and hotspots in the Yosemite Valley.
  2. Make a note of the item ID at the end of the browser's URL.

    The item ID should be 7558ee942b2547019f66885c44d4f0b1

Update the scene

  1. In Xcode, in the Project Navigator, click ViewController.swift.

  2. In the editor, modify the setupScene() method to create an AGSScene for the web scene. To do this, create a portal item providing the web scene's item ID and an AGSPortal referencing ArcGIS Online.

    ViewController.swift
    Change line
    32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 31 30 29 28 28 28 28 28 29 30 31 32 33 34 35 36 37 38 38 38 39 39 39 39 38 37 36 35 34 33 32 31 30 29 28 27 26 26 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 3 3 2 1 0 -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 -30 -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
    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
    // Copyright 2021 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.
    
    import ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var sceneView: AGSSceneView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupScene()
    
            setupUI()
    
            setupAnalysisOverlay()
    
        }
    
        // MARK: Scene
    
        private func setupScene() {
            let scene: AGSScene = {
                let portal = AGSPortal.arcGISOnline(withLoginRequired: false)
    
                let item = AGSPortalItem(portal: portal, itemID: "7558ee942b2547019f66885c44d4f0b1")
    
                return AGSScene(item: item)
            }()
            sceneView.scene = scene
    
            sceneView.touchDelegate = self
    
        }
    
        // MARK: UI
    
        private func setupUI() {
            navigationItem.title = "Long press and drag."
            let clear = UIBarButtonItem(title: "Clear", style: .plain, target: self, action: #selector(clearScene))
            navigationItem.rightBarButtonItem = clear
        }
    
        @objc
        func clearScene(_ sender: UIBarButtonItem) {
    
            clearMeasurement()
    
        }
    
        // MARK: Distance Measurement Formatter
    
        private static let measurementFormatter: MeasurementFormatter = {
            let measurementFormatter = MeasurementFormatter()
            measurementFormatter.numberFormatter.minimumFractionDigits = 2
            measurementFormatter.numberFormatter.maximumFractionDigits = 2
            return measurementFormatter
        }()
    
        private func setTitleForDistanceMeasurements(direct: AGSDistance, horizontal: AGSDistance, vertical: AGSDistance) {
            DispatchQueue.main.async { [weak self] in
                guard let self = self else { return }
                if self.measurement.isVisible {
                    self.navigationItem.title = String(
                        format: "Direct: %@ | Horizontal: %@ | Vertical: %@",
                        Self.measurementFormatter.string(from: Measurement(value: direct.value, unit: Unit(symbol: direct.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: horizontal.value, unit: Unit(symbol: horizontal.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: vertical.value, unit: Unit(symbol: vertical.unit.abbreviation)))
                    )
                } else {
                    self.navigationItem.title = "Long press and drag."
                }
            }
        }
    
        // MARK: Distance Measurement Analysis
    
        private lazy var measurement: AGSLocationDistanceMeasurement = {
            let point = AGSPointMake3D(0, 0, 0, 0, .wgs84())
            let measurement = AGSLocationDistanceMeasurement(startLocation: point, endLocation: point)
            measurement.measurementChangedHandler = setTitleForDistanceMeasurements
            measurement.isVisible = false
            measurement.unitSystem = .metric // .imperial
            return measurement
        }()
    
        private func setupAnalysisOverlay() {
            let analysisOverlay = AGSAnalysisOverlay()
            analysisOverlay.analyses.add(measurement)
            sceneView.analysisOverlays.add(analysisOverlay)
        }
    
        private func startDistanceMeasurement(point: AGSPoint) {
            measurement.isVisible = false
            measurement.startLocation = point
            measurement.endLocation = point
        }
    
        private func moveDistanceMeasurement(point: AGSPoint) {
            measurement.endLocation = point
            if !measurement.isVisible {
                measurement.isVisible = true
            }
        }
    
        private func clearMeasurement() {
            measurement.isVisible = false
            navigationItem.title = "Long press and drag."
        }
    
    }
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            startDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didMoveLongPressToScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didEndLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoViewDidCancelLongPress(_ geoView: AGSGeoView) {
    
            clearMeasurement()
    
        }
    
    }
    

Add a UI to control the distance measurement analysis

To control the distance measurement analysis, some UI is required.

  1. Define a private method named setupUI(). Create a "Clear" button and assign it to the navigation bar. The clear button removes the distance measurement analysis from the scene.

    You set an action on the clear button using a selector for a method that will be added in a later step.

    ViewController.swift
    Add line.Add line.Add line.Add line.Add line.
    48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 47 46 45 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 43 42 42 42 42 42 43 44 45 46 47 48 48 48 48 48 48 48 47 47 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 24 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -9 -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
    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
    // Copyright 2021 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.
    
    import ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var sceneView: AGSSceneView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupScene()
    
            setupUI()
    
            setupAnalysisOverlay()
    
        }
    
        // MARK: Scene
    
        private func setupScene() {
            let scene: AGSScene = {
                let portal = AGSPortal.arcGISOnline(withLoginRequired: false)
    
                let item = AGSPortalItem(portal: portal, itemID: "7558ee942b2547019f66885c44d4f0b1")
    
                return AGSScene(item: item)
            }()
            sceneView.scene = scene
    
            sceneView.touchDelegate = self
    
        }
    
        // MARK: UI
    
        private func setupUI() {
            navigationItem.title = "Long press and drag."
            let clear = UIBarButtonItem(title: "Clear", style: .plain, target: self, action: #selector(clearScene))
            navigationItem.rightBarButtonItem = clear
        }
    
        @objc
        func clearScene(_ sender: UIBarButtonItem) {
    
            clearMeasurement()
    
        }
    
        // MARK: Distance Measurement Formatter
    
        private static let measurementFormatter: MeasurementFormatter = {
            let measurementFormatter = MeasurementFormatter()
            measurementFormatter.numberFormatter.minimumFractionDigits = 2
            measurementFormatter.numberFormatter.maximumFractionDigits = 2
            return measurementFormatter
        }()
    
        private func setTitleForDistanceMeasurements(direct: AGSDistance, horizontal: AGSDistance, vertical: AGSDistance) {
            DispatchQueue.main.async { [weak self] in
                guard let self = self else { return }
                if self.measurement.isVisible {
                    self.navigationItem.title = String(
                        format: "Direct: %@ | Horizontal: %@ | Vertical: %@",
                        Self.measurementFormatter.string(from: Measurement(value: direct.value, unit: Unit(symbol: direct.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: horizontal.value, unit: Unit(symbol: horizontal.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: vertical.value, unit: Unit(symbol: vertical.unit.abbreviation)))
                    )
                } else {
                    self.navigationItem.title = "Long press and drag."
                }
            }
        }
    
        // MARK: Distance Measurement Analysis
    
        private lazy var measurement: AGSLocationDistanceMeasurement = {
            let point = AGSPointMake3D(0, 0, 0, 0, .wgs84())
            let measurement = AGSLocationDistanceMeasurement(startLocation: point, endLocation: point)
            measurement.measurementChangedHandler = setTitleForDistanceMeasurements
            measurement.isVisible = false
            measurement.unitSystem = .metric // .imperial
            return measurement
        }()
    
        private func setupAnalysisOverlay() {
            let analysisOverlay = AGSAnalysisOverlay()
            analysisOverlay.analyses.add(measurement)
            sceneView.analysisOverlays.add(analysisOverlay)
        }
    
        private func startDistanceMeasurement(point: AGSPoint) {
            measurement.isVisible = false
            measurement.startLocation = point
            measurement.endLocation = point
        }
    
        private func moveDistanceMeasurement(point: AGSPoint) {
            measurement.endLocation = point
            if !measurement.isVisible {
                measurement.isVisible = true
            }
        }
    
        private func clearMeasurement() {
            measurement.isVisible = false
            navigationItem.title = "Long press and drag."
        }
    
    }
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            startDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didMoveLongPressToScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didEndLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoViewDidCancelLongPress(_ geoView: AGSGeoView) {
    
            clearMeasurement()
    
        }
    
    }
    
  2. Define a method named clearScene(_:) and assign it the @objc keyword. This method is performed when the user taps the clear button.

    The @objc method keyword exposes the method to Objective-C, a necessary step for using the UIBarButtonItem API.

    ViewController.swift
    Add line.Add line.Add line.Add line.
    48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 47 46 45 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 43 42 42 42 42 42 43 44 45 46 47 48 49 50 51 51 51 52 52 52 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 29 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -4 -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
    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
    // Copyright 2021 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.
    
    import ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var sceneView: AGSSceneView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupScene()
    
            setupUI()
    
            setupAnalysisOverlay()
    
        }
    
        // MARK: Scene
    
        private func setupScene() {
            let scene: AGSScene = {
                let portal = AGSPortal.arcGISOnline(withLoginRequired: false)
    
                let item = AGSPortalItem(portal: portal, itemID: "7558ee942b2547019f66885c44d4f0b1")
    
                return AGSScene(item: item)
            }()
            sceneView.scene = scene
    
            sceneView.touchDelegate = self
    
        }
    
        // MARK: UI
    
        private func setupUI() {
            navigationItem.title = "Long press and drag."
            let clear = UIBarButtonItem(title: "Clear", style: .plain, target: self, action: #selector(clearScene))
            navigationItem.rightBarButtonItem = clear
        }
    
        @objc
        func clearScene(_ sender: UIBarButtonItem) {
    
            clearMeasurement()
    
        }
    
        // MARK: Distance Measurement Formatter
    
        private static let measurementFormatter: MeasurementFormatter = {
            let measurementFormatter = MeasurementFormatter()
            measurementFormatter.numberFormatter.minimumFractionDigits = 2
            measurementFormatter.numberFormatter.maximumFractionDigits = 2
            return measurementFormatter
        }()
    
        private func setTitleForDistanceMeasurements(direct: AGSDistance, horizontal: AGSDistance, vertical: AGSDistance) {
            DispatchQueue.main.async { [weak self] in
                guard let self = self else { return }
                if self.measurement.isVisible {
                    self.navigationItem.title = String(
                        format: "Direct: %@ | Horizontal: %@ | Vertical: %@",
                        Self.measurementFormatter.string(from: Measurement(value: direct.value, unit: Unit(symbol: direct.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: horizontal.value, unit: Unit(symbol: horizontal.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: vertical.value, unit: Unit(symbol: vertical.unit.abbreviation)))
                    )
                } else {
                    self.navigationItem.title = "Long press and drag."
                }
            }
        }
    
        // MARK: Distance Measurement Analysis
    
        private lazy var measurement: AGSLocationDistanceMeasurement = {
            let point = AGSPointMake3D(0, 0, 0, 0, .wgs84())
            let measurement = AGSLocationDistanceMeasurement(startLocation: point, endLocation: point)
            measurement.measurementChangedHandler = setTitleForDistanceMeasurements
            measurement.isVisible = false
            measurement.unitSystem = .metric // .imperial
            return measurement
        }()
    
        private func setupAnalysisOverlay() {
            let analysisOverlay = AGSAnalysisOverlay()
            analysisOverlay.analyses.add(measurement)
            sceneView.analysisOverlays.add(analysisOverlay)
        }
    
        private func startDistanceMeasurement(point: AGSPoint) {
            measurement.isVisible = false
            measurement.startLocation = point
            measurement.endLocation = point
        }
    
        private func moveDistanceMeasurement(point: AGSPoint) {
            measurement.endLocation = point
            if !measurement.isVisible {
                measurement.isVisible = true
            }
        }
    
        private func clearMeasurement() {
            measurement.isVisible = false
            navigationItem.title = "Long press and drag."
        }
    
    }
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            startDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didMoveLongPressToScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didEndLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoViewDidCancelLongPress(_ geoView: AGSGeoView) {
    
            clearMeasurement()
    
        }
    
    }
    
  3. In viewDidLoad(), call the method setupUI().

    ViewController.swift
    Add line.
    20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 22 23 24 25 26 26 26 27 27 27 27 27 27 27 27 27 27 27 27 27 27 26 25 25 25 25 25 25 25 25 25 25 25 25 25 25 24 23 23 23 23 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 0 -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 -33 -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
    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
    // Copyright 2021 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.
    
    import ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var sceneView: AGSSceneView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupScene()
    
            setupUI()
    
            setupAnalysisOverlay()
    
        }
    
        // MARK: Scene
    
        private func setupScene() {
            let scene: AGSScene = {
                let portal = AGSPortal.arcGISOnline(withLoginRequired: false)
    
                let item = AGSPortalItem(portal: portal, itemID: "7558ee942b2547019f66885c44d4f0b1")
    
                return AGSScene(item: item)
            }()
            sceneView.scene = scene
    
            sceneView.touchDelegate = self
    
        }
    
        // MARK: UI
    
        private func setupUI() {
            navigationItem.title = "Long press and drag."
            let clear = UIBarButtonItem(title: "Clear", style: .plain, target: self, action: #selector(clearScene))
            navigationItem.rightBarButtonItem = clear
        }
    
        @objc
        func clearScene(_ sender: UIBarButtonItem) {
    
            clearMeasurement()
    
        }
    
        // MARK: Distance Measurement Formatter
    
        private static let measurementFormatter: MeasurementFormatter = {
            let measurementFormatter = MeasurementFormatter()
            measurementFormatter.numberFormatter.minimumFractionDigits = 2
            measurementFormatter.numberFormatter.maximumFractionDigits = 2
            return measurementFormatter
        }()
    
        private func setTitleForDistanceMeasurements(direct: AGSDistance, horizontal: AGSDistance, vertical: AGSDistance) {
            DispatchQueue.main.async { [weak self] in
                guard let self = self else { return }
                if self.measurement.isVisible {
                    self.navigationItem.title = String(
                        format: "Direct: %@ | Horizontal: %@ | Vertical: %@",
                        Self.measurementFormatter.string(from: Measurement(value: direct.value, unit: Unit(symbol: direct.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: horizontal.value, unit: Unit(symbol: horizontal.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: vertical.value, unit: Unit(symbol: vertical.unit.abbreviation)))
                    )
                } else {
                    self.navigationItem.title = "Long press and drag."
                }
            }
        }
    
        // MARK: Distance Measurement Analysis
    
        private lazy var measurement: AGSLocationDistanceMeasurement = {
            let point = AGSPointMake3D(0, 0, 0, 0, .wgs84())
            let measurement = AGSLocationDistanceMeasurement(startLocation: point, endLocation: point)
            measurement.measurementChangedHandler = setTitleForDistanceMeasurements
            measurement.isVisible = false
            measurement.unitSystem = .metric // .imperial
            return measurement
        }()
    
        private func setupAnalysisOverlay() {
            let analysisOverlay = AGSAnalysisOverlay()
            analysisOverlay.analyses.add(measurement)
            sceneView.analysisOverlays.add(analysisOverlay)
        }
    
        private func startDistanceMeasurement(point: AGSPoint) {
            measurement.isVisible = false
            measurement.startLocation = point
            measurement.endLocation = point
        }
    
        private func moveDistanceMeasurement(point: AGSPoint) {
            measurement.endLocation = point
            if !measurement.isVisible {
                measurement.isVisible = true
            }
        }
    
        private func clearMeasurement() {
            measurement.isVisible = false
            navigationItem.title = "Long press and drag."
        }
    
    }
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            startDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didMoveLongPressToScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didEndLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoViewDidCancelLongPress(_ geoView: AGSGeoView) {
    
            clearMeasurement()
    
        }
    
    }
    
  4. In Xcode, in the Project Navigator, click Main.storyboard.

  5. In the editor, select ViewController. In the menu bar, click Editor > Embed In > Navigation Controller.

    Embedding ViewController within a Navigation Controller will place a navigation bar at the top of ViewController. Inside the navigation bar you will find the UI elements.

  6. Check that your UI is set up properly. Press <Command+R> to run the app. You should see a "Clear" button contained by the navigation bar, the default title should be displayed, and the app should load the Yosemite Valley Hotspots web scene.

Format measurements for readability

  1. In Xcode, in the Project Navigator, click ViewController.swift.

  2. In the editor, create a private static MeasurementFormatter named measurementFormatter. The measurement formatter is used to convert measurement values into strings that are in a format localized to the user's device.

    ViewController.swift
    Add line.Add line.Add line.Add line.Add line.Add line.
    63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 62 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 60 59 59 59 59 59 59 59 59 59 59 59 59 59 59 58 57 57 57 57 57 58 59 60 61 62 63 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 63 63 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 30 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3
    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
    // Copyright 2021 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.
    
    import ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var sceneView: AGSSceneView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupScene()
    
            setupUI()
    
            setupAnalysisOverlay()
    
        }
    
        // MARK: Scene
    
        private func setupScene() {
            let scene: AGSScene = {
                let portal = AGSPortal.arcGISOnline(withLoginRequired: false)
    
                let item = AGSPortalItem(portal: portal, itemID: "7558ee942b2547019f66885c44d4f0b1")
    
                return AGSScene(item: item)
            }()
            sceneView.scene = scene
    
            sceneView.touchDelegate = self
    
        }
    
        // MARK: UI
    
        private func setupUI() {
            navigationItem.title = "Long press and drag."
            let clear = UIBarButtonItem(title: "Clear", style: .plain, target: self, action: #selector(clearScene))
            navigationItem.rightBarButtonItem = clear
        }
    
        @objc
        func clearScene(_ sender: UIBarButtonItem) {
    
            clearMeasurement()
    
        }
    
        // MARK: Distance Measurement Formatter
    
        private static let measurementFormatter: MeasurementFormatter = {
            let measurementFormatter = MeasurementFormatter()
            measurementFormatter.numberFormatter.minimumFractionDigits = 2
            measurementFormatter.numberFormatter.maximumFractionDigits = 2
            return measurementFormatter
        }()
    
        private func setTitleForDistanceMeasurements(direct: AGSDistance, horizontal: AGSDistance, vertical: AGSDistance) {
            DispatchQueue.main.async { [weak self] in
                guard let self = self else { return }
                if self.measurement.isVisible {
                    self.navigationItem.title = String(
                        format: "Direct: %@ | Horizontal: %@ | Vertical: %@",
                        Self.measurementFormatter.string(from: Measurement(value: direct.value, unit: Unit(symbol: direct.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: horizontal.value, unit: Unit(symbol: horizontal.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: vertical.value, unit: Unit(symbol: vertical.unit.abbreviation)))
                    )
                } else {
                    self.navigationItem.title = "Long press and drag."
                }
            }
        }
    
        // MARK: Distance Measurement Analysis
    
        private lazy var measurement: AGSLocationDistanceMeasurement = {
            let point = AGSPointMake3D(0, 0, 0, 0, .wgs84())
            let measurement = AGSLocationDistanceMeasurement(startLocation: point, endLocation: point)
            measurement.measurementChangedHandler = setTitleForDistanceMeasurements
            measurement.isVisible = false
            measurement.unitSystem = .metric // .imperial
            return measurement
        }()
    
        private func setupAnalysisOverlay() {
            let analysisOverlay = AGSAnalysisOverlay()
            analysisOverlay.analyses.add(measurement)
            sceneView.analysisOverlays.add(analysisOverlay)
        }
    
        private func startDistanceMeasurement(point: AGSPoint) {
            measurement.isVisible = false
            measurement.startLocation = point
            measurement.endLocation = point
        }
    
        private func moveDistanceMeasurement(point: AGSPoint) {
            measurement.endLocation = point
            if !measurement.isVisible {
                measurement.isVisible = true
            }
        }
    
        private func clearMeasurement() {
            measurement.isVisible = false
            navigationItem.title = "Long press and drag."
        }
    
    }
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            startDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didMoveLongPressToScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didEndLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoViewDidCancelLongPress(_ geoView: AGSGeoView) {
    
            clearMeasurement()
    
        }
    
    }
    
  3. Define a private method named setTitleForDistanceMeasurements(direct:horizontal:vertical:). This method is used to format a string to communicate the direct, horizontal, and vertical measurements to the user, placing the results in the navigation bar's title.

    Because setTitleForDistanceMeasurements performs a UI operation and you cannot guarantee that it will be called from the main thread, you must ensure that the operation is performed asynchronously on the main thread.

    ViewController.swift
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 62 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 60 59 59 59 59 59 59 59 59 59 59 59 59 59 59 58 57 57 57 57 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 79 79 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 46 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19
    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
    // Copyright 2021 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.
    
    import ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var sceneView: AGSSceneView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupScene()
    
            setupUI()
    
            setupAnalysisOverlay()
    
        }
    
        // MARK: Scene
    
        private func setupScene() {
            let scene: AGSScene = {
                let portal = AGSPortal.arcGISOnline(withLoginRequired: false)
    
                let item = AGSPortalItem(portal: portal, itemID: "7558ee942b2547019f66885c44d4f0b1")
    
                return AGSScene(item: item)
            }()
            sceneView.scene = scene
    
            sceneView.touchDelegate = self
    
        }
    
        // MARK: UI
    
        private func setupUI() {
            navigationItem.title = "Long press and drag."
            let clear = UIBarButtonItem(title: "Clear", style: .plain, target: self, action: #selector(clearScene))
            navigationItem.rightBarButtonItem = clear
        }
    
        @objc
        func clearScene(_ sender: UIBarButtonItem) {
    
            clearMeasurement()
    
        }
    
        // MARK: Distance Measurement Formatter
    
        private static let measurementFormatter: MeasurementFormatter = {
            let measurementFormatter = MeasurementFormatter()
            measurementFormatter.numberFormatter.minimumFractionDigits = 2
            measurementFormatter.numberFormatter.maximumFractionDigits = 2
            return measurementFormatter
        }()
    
        private func setTitleForDistanceMeasurements(direct: AGSDistance, horizontal: AGSDistance, vertical: AGSDistance) {
            DispatchQueue.main.async { [weak self] in
                guard let self = self else { return }
                if self.measurement.isVisible {
                    self.navigationItem.title = String(
                        format: "Direct: %@ | Horizontal: %@ | Vertical: %@",
                        Self.measurementFormatter.string(from: Measurement(value: direct.value, unit: Unit(symbol: direct.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: horizontal.value, unit: Unit(symbol: horizontal.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: vertical.value, unit: Unit(symbol: vertical.unit.abbreviation)))
                    )
                } else {
                    self.navigationItem.title = "Long press and drag."
                }
            }
        }
    
        // MARK: Distance Measurement Analysis
    
        private lazy var measurement: AGSLocationDistanceMeasurement = {
            let point = AGSPointMake3D(0, 0, 0, 0, .wgs84())
            let measurement = AGSLocationDistanceMeasurement(startLocation: point, endLocation: point)
            measurement.measurementChangedHandler = setTitleForDistanceMeasurements
            measurement.isVisible = false
            measurement.unitSystem = .metric // .imperial
            return measurement
        }()
    
        private func setupAnalysisOverlay() {
            let analysisOverlay = AGSAnalysisOverlay()
            analysisOverlay.analyses.add(measurement)
            sceneView.analysisOverlays.add(analysisOverlay)
        }
    
        private func startDistanceMeasurement(point: AGSPoint) {
            measurement.isVisible = false
            measurement.startLocation = point
            measurement.endLocation = point
        }
    
        private func moveDistanceMeasurement(point: AGSPoint) {
            measurement.endLocation = point
            if !measurement.isVisible {
                measurement.isVisible = true
            }
        }
    
        private func clearMeasurement() {
            measurement.isVisible = false
            navigationItem.title = "Long press and drag."
        }
    
    }
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            startDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didMoveLongPressToScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didEndLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoViewDidCancelLongPress(_ geoView: AGSGeoView) {
    
            clearMeasurement()
    
        }
    
    }
    

Create a distance measurement analysis

Use a AGSLocationDistanceMeasurement to perform and display a distance measurement analysis using 3D points to define the start and end locations.

  1. Create a private lazy AGSLocationDistanceMeasurement named measurement. Upon launch, the distance measurement visualization should not be visible.

    ViewController.swift
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 87 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 85 84 84 84 84 84 84 84 84 84 84 84 84 84 84 83 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 83 84 85 86 87 88 89 90 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 90 90 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63
    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
    // Copyright 2021 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.
    
    import ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var sceneView: AGSSceneView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupScene()
    
            setupUI()
    
            setupAnalysisOverlay()
    
        }
    
        // MARK: Scene
    
        private func setupScene() {
            let scene: AGSScene = {
                let portal = AGSPortal.arcGISOnline(withLoginRequired: false)
    
                let item = AGSPortalItem(portal: portal, itemID: "7558ee942b2547019f66885c44d4f0b1")
    
                return AGSScene(item: item)
            }()
            sceneView.scene = scene
    
            sceneView.touchDelegate = self
    
        }
    
        // MARK: UI
    
        private func setupUI() {
            navigationItem.title = "Long press and drag."
            let clear = UIBarButtonItem(title: "Clear", style: .plain, target: self, action: #selector(clearScene))
            navigationItem.rightBarButtonItem = clear
        }
    
        @objc
        func clearScene(_ sender: UIBarButtonItem) {
    
            clearMeasurement()
    
        }
    
        // MARK: Distance Measurement Formatter
    
        private static let measurementFormatter: MeasurementFormatter = {
            let measurementFormatter = MeasurementFormatter()
            measurementFormatter.numberFormatter.minimumFractionDigits = 2
            measurementFormatter.numberFormatter.maximumFractionDigits = 2
            return measurementFormatter
        }()
    
        private func setTitleForDistanceMeasurements(direct: AGSDistance, horizontal: AGSDistance, vertical: AGSDistance) {
            DispatchQueue.main.async { [weak self] in
                guard let self = self else { return }
                if self.measurement.isVisible {
                    self.navigationItem.title = String(
                        format: "Direct: %@ | Horizontal: %@ | Vertical: %@",
                        Self.measurementFormatter.string(from: Measurement(value: direct.value, unit: Unit(symbol: direct.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: horizontal.value, unit: Unit(symbol: horizontal.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: vertical.value, unit: Unit(symbol: vertical.unit.abbreviation)))
                    )
                } else {
                    self.navigationItem.title = "Long press and drag."
                }
            }
        }
    
        // MARK: Distance Measurement Analysis
    
        private lazy var measurement: AGSLocationDistanceMeasurement = {
            let point = AGSPointMake3D(0, 0, 0, 0, .wgs84())
            let measurement = AGSLocationDistanceMeasurement(startLocation: point, endLocation: point)
            measurement.measurementChangedHandler = setTitleForDistanceMeasurements
            measurement.isVisible = false
            measurement.unitSystem = .metric // .imperial
            return measurement
        }()
    
        private func setupAnalysisOverlay() {
            let analysisOverlay = AGSAnalysisOverlay()
            analysisOverlay.analyses.add(measurement)
            sceneView.analysisOverlays.add(analysisOverlay)
        }
    
        private func startDistanceMeasurement(point: AGSPoint) {
            measurement.isVisible = false
            measurement.startLocation = point
            measurement.endLocation = point
        }
    
        private func moveDistanceMeasurement(point: AGSPoint) {
            measurement.endLocation = point
            if !measurement.isVisible {
                measurement.isVisible = true
            }
        }
    
        private func clearMeasurement() {
            measurement.isVisible = false
            navigationItem.title = "Long press and drag."
        }
    
    }
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            startDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didMoveLongPressToScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didEndLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoViewDidCancelLongPress(_ geoView: AGSGeoView) {
    
            clearMeasurement()
    
        }
    
    }
    
  2. Define a private method named setupAnalysisOverlay() to add the distance measurement analysis to the scene.

    The distance measurement analysis is added to a scene view using an analysis overlay. An analysis overlay is a container for analyses. It can be used to display visual analyses in a scene view. You can add more than one analysis overlay and they are displayed on top of all other layers.

    ViewController.swift
    Add line.Add line.Add line.Add line.Add line.
    88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 87 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 85 84 84 84 84 84 84 84 84 84 84 84 84 84 84 83 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 78 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51
    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
    // Copyright 2021 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.
    
    import ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var sceneView: AGSSceneView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupScene()
    
            setupUI()
    
            setupAnalysisOverlay()
    
        }
    
        // MARK: Scene
    
        private func setupScene() {
            let scene: AGSScene = {
                let portal = AGSPortal.arcGISOnline(withLoginRequired: false)
    
                let item = AGSPortalItem(portal: portal, itemID: "7558ee942b2547019f66885c44d4f0b1")
    
                return AGSScene(item: item)
            }()
            sceneView.scene = scene
    
            sceneView.touchDelegate = self
    
        }
    
        // MARK: UI
    
        private func setupUI() {
            navigationItem.title = "Long press and drag."
            let clear = UIBarButtonItem(title: "Clear", style: .plain, target: self, action: #selector(clearScene))
            navigationItem.rightBarButtonItem = clear
        }
    
        @objc
        func clearScene(_ sender: UIBarButtonItem) {
    
            clearMeasurement()
    
        }
    
        // MARK: Distance Measurement Formatter
    
        private static let measurementFormatter: MeasurementFormatter = {
            let measurementFormatter = MeasurementFormatter()
            measurementFormatter.numberFormatter.minimumFractionDigits = 2
            measurementFormatter.numberFormatter.maximumFractionDigits = 2
            return measurementFormatter
        }()
    
        private func setTitleForDistanceMeasurements(direct: AGSDistance, horizontal: AGSDistance, vertical: AGSDistance) {
            DispatchQueue.main.async { [weak self] in
                guard let self = self else { return }
                if self.measurement.isVisible {
                    self.navigationItem.title = String(
                        format: "Direct: %@ | Horizontal: %@ | Vertical: %@",
                        Self.measurementFormatter.string(from: Measurement(value: direct.value, unit: Unit(symbol: direct.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: horizontal.value, unit: Unit(symbol: horizontal.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: vertical.value, unit: Unit(symbol: vertical.unit.abbreviation)))
                    )
                } else {
                    self.navigationItem.title = "Long press and drag."
                }
            }
        }
    
        // MARK: Distance Measurement Analysis
    
        private lazy var measurement: AGSLocationDistanceMeasurement = {
            let point = AGSPointMake3D(0, 0, 0, 0, .wgs84())
            let measurement = AGSLocationDistanceMeasurement(startLocation: point, endLocation: point)
            measurement.measurementChangedHandler = setTitleForDistanceMeasurements
            measurement.isVisible = false
            measurement.unitSystem = .metric // .imperial
            return measurement
        }()
    
        private func setupAnalysisOverlay() {
            let analysisOverlay = AGSAnalysisOverlay()
            analysisOverlay.analyses.add(measurement)
            sceneView.analysisOverlays.add(analysisOverlay)
        }
    
        private func startDistanceMeasurement(point: AGSPoint) {
            measurement.isVisible = false
            measurement.startLocation = point
            measurement.endLocation = point
        }
    
        private func moveDistanceMeasurement(point: AGSPoint) {
            measurement.endLocation = point
            if !measurement.isVisible {
                measurement.isVisible = true
            }
        }
    
        private func clearMeasurement() {
            measurement.isVisible = false
            navigationItem.title = "Long press and drag."
        }
    
    }
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            startDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didMoveLongPressToScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didEndLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoViewDidCancelLongPress(_ geoView: AGSGeoView) {
    
            clearMeasurement()
    
        }
    
    }
    
  3. Define a private method named startDistanceMeasurement(point:) that receives a point as a parameter. This method is used to set the distance measurement analysis startLocation.

    ViewController.swift
    Add line.Add line.Add line.Add line.Add line.
    97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 96 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 94 93 93 93 93 93 93 93 93 93 93 93 93 93 93 92 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 92 93 94 95 96 97 98 99 100 101 102 102 101 100 99 98 97 96 95 94 93 92 91 90 90 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63
    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
    // Copyright 2021 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.
    
    import ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var sceneView: AGSSceneView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupScene()
    
            setupUI()
    
            setupAnalysisOverlay()
    
        }
    
        // MARK: Scene
    
        private func setupScene() {
            let scene: AGSScene = {
                let portal = AGSPortal.arcGISOnline(withLoginRequired: false)
    
                let item = AGSPortalItem(portal: portal, itemID: "7558ee942b2547019f66885c44d4f0b1")
    
                return AGSScene(item: item)
            }()
            sceneView.scene = scene
    
            sceneView.touchDelegate = self
    
        }
    
        // MARK: UI
    
        private func setupUI() {
            navigationItem.title = "Long press and drag."
            let clear = UIBarButtonItem(title: "Clear", style: .plain, target: self, action: #selector(clearScene))
            navigationItem.rightBarButtonItem = clear
        }
    
        @objc
        func clearScene(_ sender: UIBarButtonItem) {
    
            clearMeasurement()
    
        }
    
        // MARK: Distance Measurement Formatter
    
        private static let measurementFormatter: MeasurementFormatter = {
            let measurementFormatter = MeasurementFormatter()
            measurementFormatter.numberFormatter.minimumFractionDigits = 2
            measurementFormatter.numberFormatter.maximumFractionDigits = 2
            return measurementFormatter
        }()
    
        private func setTitleForDistanceMeasurements(direct: AGSDistance, horizontal: AGSDistance, vertical: AGSDistance) {
            DispatchQueue.main.async { [weak self] in
                guard let self = self else { return }
                if self.measurement.isVisible {
                    self.navigationItem.title = String(
                        format: "Direct: %@ | Horizontal: %@ | Vertical: %@",
                        Self.measurementFormatter.string(from: Measurement(value: direct.value, unit: Unit(symbol: direct.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: horizontal.value, unit: Unit(symbol: horizontal.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: vertical.value, unit: Unit(symbol: vertical.unit.abbreviation)))
                    )
                } else {
                    self.navigationItem.title = "Long press and drag."
                }
            }
        }
    
        // MARK: Distance Measurement Analysis
    
        private lazy var measurement: AGSLocationDistanceMeasurement = {
            let point = AGSPointMake3D(0, 0, 0, 0, .wgs84())
            let measurement = AGSLocationDistanceMeasurement(startLocation: point, endLocation: point)
            measurement.measurementChangedHandler = setTitleForDistanceMeasurements
            measurement.isVisible = false
            measurement.unitSystem = .metric // .imperial
            return measurement
        }()
    
        private func setupAnalysisOverlay() {
            let analysisOverlay = AGSAnalysisOverlay()
            analysisOverlay.analyses.add(measurement)
            sceneView.analysisOverlays.add(analysisOverlay)
        }
    
        private func startDistanceMeasurement(point: AGSPoint) {
            measurement.isVisible = false
            measurement.startLocation = point
            measurement.endLocation = point
        }
    
        private func moveDistanceMeasurement(point: AGSPoint) {
            measurement.endLocation = point
            if !measurement.isVisible {
                measurement.isVisible = true
            }
        }
    
        private func clearMeasurement() {
            measurement.isVisible = false
            navigationItem.title = "Long press and drag."
        }
    
    }
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            startDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didMoveLongPressToScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didEndLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoViewDidCancelLongPress(_ geoView: AGSGeoView) {
    
            clearMeasurement()
    
        }
    
    }
    
  4. Define a private method named moveDistanceMeasurement(point:) that receives a point as a parameter. This method is used to set the distance measurement analysis endLocation and make it visible, if it's not visible already.

    ViewController.swift
    Add line.Add line.Add line.Add line.Add line.Add line.
    103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 102 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 100 99 99 99 99 99 99 99 99 99 99 99 99 99 99 98 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 98 99 100 101 102 103 104 105 106 107 108 109 109 108 107 106 105 104 104 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77
    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
    // Copyright 2021 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.
    
    import ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var sceneView: AGSSceneView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupScene()
    
            setupUI()
    
            setupAnalysisOverlay()
    
        }
    
        // MARK: Scene
    
        private func setupScene() {
            let scene: AGSScene = {
                let portal = AGSPortal.arcGISOnline(withLoginRequired: false)
    
                let item = AGSPortalItem(portal: portal, itemID: "7558ee942b2547019f66885c44d4f0b1")
    
                return AGSScene(item: item)
            }()
            sceneView.scene = scene
    
            sceneView.touchDelegate = self
    
        }
    
        // MARK: UI
    
        private func setupUI() {
            navigationItem.title = "Long press and drag."
            let clear = UIBarButtonItem(title: "Clear", style: .plain, target: self, action: #selector(clearScene))
            navigationItem.rightBarButtonItem = clear
        }
    
        @objc
        func clearScene(_ sender: UIBarButtonItem) {
    
            clearMeasurement()
    
        }
    
        // MARK: Distance Measurement Formatter
    
        private static let measurementFormatter: MeasurementFormatter = {
            let measurementFormatter = MeasurementFormatter()
            measurementFormatter.numberFormatter.minimumFractionDigits = 2
            measurementFormatter.numberFormatter.maximumFractionDigits = 2
            return measurementFormatter
        }()
    
        private func setTitleForDistanceMeasurements(direct: AGSDistance, horizontal: AGSDistance, vertical: AGSDistance) {
            DispatchQueue.main.async { [weak self] in
                guard let self = self else { return }
                if self.measurement.isVisible {
                    self.navigationItem.title = String(
                        format: "Direct: %@ | Horizontal: %@ | Vertical: %@",
                        Self.measurementFormatter.string(from: Measurement(value: direct.value, unit: Unit(symbol: direct.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: horizontal.value, unit: Unit(symbol: horizontal.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: vertical.value, unit: Unit(symbol: vertical.unit.abbreviation)))
                    )
                } else {
                    self.navigationItem.title = "Long press and drag."
                }
            }
        }
    
        // MARK: Distance Measurement Analysis
    
        private lazy var measurement: AGSLocationDistanceMeasurement = {
            let point = AGSPointMake3D(0, 0, 0, 0, .wgs84())
            let measurement = AGSLocationDistanceMeasurement(startLocation: point, endLocation: point)
            measurement.measurementChangedHandler = setTitleForDistanceMeasurements
            measurement.isVisible = false
            measurement.unitSystem = .metric // .imperial
            return measurement
        }()
    
        private func setupAnalysisOverlay() {
            let analysisOverlay = AGSAnalysisOverlay()
            analysisOverlay.analyses.add(measurement)
            sceneView.analysisOverlays.add(analysisOverlay)
        }
    
        private func startDistanceMeasurement(point: AGSPoint) {
            measurement.isVisible = false
            measurement.startLocation = point
            measurement.endLocation = point
        }
    
        private func moveDistanceMeasurement(point: AGSPoint) {
            measurement.endLocation = point
            if !measurement.isVisible {
                measurement.isVisible = true
            }
        }
    
        private func clearMeasurement() {
            measurement.isVisible = false
            navigationItem.title = "Long press and drag."
        }
    
    }
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            startDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didMoveLongPressToScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didEndLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoViewDidCancelLongPress(_ geoView: AGSGeoView) {
    
            clearMeasurement()
    
        }
    
    }
    
  5. Define a private method named clearMeasurement(). This method is used to hide the distance measurement analysis.

    ViewController.swift
    Add line.Add line.Add line.Add line.
    109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 108 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 106 105 105 105 105 105 105 105 105 105 105 105 105 105 105 104 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 104 105 106 107 108 109 110 111 112 113 114 114 114 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87
    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
    // Copyright 2021 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.
    
    import ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var sceneView: AGSSceneView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupScene()
    
            setupUI()
    
            setupAnalysisOverlay()
    
        }
    
        // MARK: Scene
    
        private func setupScene() {
            let scene: AGSScene = {
                let portal = AGSPortal.arcGISOnline(withLoginRequired: false)
    
                let item = AGSPortalItem(portal: portal, itemID: "7558ee942b2547019f66885c44d4f0b1")
    
                return AGSScene(item: item)
            }()
            sceneView.scene = scene
    
            sceneView.touchDelegate = self
    
        }
    
        // MARK: UI
    
        private func setupUI() {
            navigationItem.title = "Long press and drag."
            let clear = UIBarButtonItem(title: "Clear", style: .plain, target: self, action: #selector(clearScene))
            navigationItem.rightBarButtonItem = clear
        }
    
        @objc
        func clearScene(_ sender: UIBarButtonItem) {
    
            clearMeasurement()
    
        }
    
        // MARK: Distance Measurement Formatter
    
        private static let measurementFormatter: MeasurementFormatter = {
            let measurementFormatter = MeasurementFormatter()
            measurementFormatter.numberFormatter.minimumFractionDigits = 2
            measurementFormatter.numberFormatter.maximumFractionDigits = 2
            return measurementFormatter
        }()
    
        private func setTitleForDistanceMeasurements(direct: AGSDistance, horizontal: AGSDistance, vertical: AGSDistance) {
            DispatchQueue.main.async { [weak self] in
                guard let self = self else { return }
                if self.measurement.isVisible {
                    self.navigationItem.title = String(
                        format: "Direct: %@ | Horizontal: %@ | Vertical: %@",
                        Self.measurementFormatter.string(from: Measurement(value: direct.value, unit: Unit(symbol: direct.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: horizontal.value, unit: Unit(symbol: horizontal.unit.abbreviation))),
                        Self.measurementFormatter.string(from: Measurement(value: vertical.value, unit: Unit(symbol: vertical.unit.abbreviation)))
                    )
                } else {
                    self.navigationItem.title = "Long press and drag."
                }
            }
        }
    
        // MARK: Distance Measurement Analysis
    
        private lazy var measurement: AGSLocationDistanceMeasurement = {
            let point = AGSPointMake3D(0, 0, 0, 0, .wgs84())
            let measurement = AGSLocationDistanceMeasurement(startLocation: point, endLocation: point)
            measurement.measurementChangedHandler = setTitleForDistanceMeasurements
            measurement.isVisible = false
            measurement.unitSystem = .metric // .imperial
            return measurement
        }()
    
        private func setupAnalysisOverlay() {
            let analysisOverlay = AGSAnalysisOverlay()
            analysisOverlay.analyses.add(measurement)
            sceneView.analysisOverlays.add(analysisOverlay)
        }
    
        private func startDistanceMeasurement(point: AGSPoint) {
            measurement.isVisible = false
            measurement.startLocation = point
            measurement.endLocation = point
        }
    
        private func moveDistanceMeasurement(point: AGSPoint) {
            measurement.endLocation = point
            if !measurement.isVisible {
                measurement.isVisible = true
            }
        }
    
        private func clearMeasurement() {
            measurement.isVisible = false
            navigationItem.title = "Long press and drag."
        }
    
    }
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            startDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didMoveLongPressToScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didEndLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            moveDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoViewDidCancelLongPress(_ geoView: AGSGeoView) {
    
            clearMeasurement()
    
        }
    
    }
    
  6. Call clearMeasurement() when the user taps the "Clear" button.

    ViewController.swift
    Add line.
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 53 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 51 50 50 50 50 50 50 50 50 50 50 50 51 52 53 54 55 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 55 54 53 52 51 50 49 48 47 46 45 44