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

Before starting this tutorial:

  1. You need an ArcGIS Location Platform or ArcGIS Online account.

  2. Your system meets the system requirements.

Steps

Open the Xcode project

  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.

  3. If you downloaded the solution, get an access token and set the API key.

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
    Expand
    Use dark colors for code blocks
    29 30 31 32 33 34 35 36 37 38 39
    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
    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
        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
    
        }
    
    Expand

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
    Expand
    Use dark colors for code blocks
    43 44 45 46 47 48
    Add line.Add line.Add line.Add line.Add 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
    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
        private func setupUI() {
            navigationItem.title = "Long press and drag."
            let clear = UIBarButtonItem(title: "Clear", style: .plain, target: self, action: #selector(clearScene))
            navigationItem.rightBarButtonItem = clear
        }
    
    
    Expand
  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
    Expand
    Use dark colors for code blocks
    43 44 45 46 47 48 49 50 51 52
    Add line.Add line.Add line.Add 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
    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
        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) {
    
        }
    
    Expand
  3. In viewDidLoad(), call the method setupUI().

    ViewController.swift
    Expand
    Use dark colors for code blocks
    21 22 23 24 25 26 27
    Add 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
    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
        override func viewDidLoad() {
            super.viewDidLoad()
            setupScene()
    
            setupUI()
    
        }
    
    Expand
  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.

  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
    Expand
    Use dark colors for code blocks
    58 59 60 61 62 63 64
    Add line.Add line.Add line.Add line.Add line.Add 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
    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
        private static let measurementFormatter: MeasurementFormatter = {
            let measurementFormatter = MeasurementFormatter()
            measurementFormatter.numberFormatter.minimumFractionDigits = 2
            measurementFormatter.numberFormatter.maximumFractionDigits = 2
            return measurementFormatter
        }()
    
    
    Expand
  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.

    ViewController.swift
    Expand
    Use dark colors for code blocks
    58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    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.
    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
        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."
                }
            }
        }
    
    Expand

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
    Expand
    Use dark colors for code blocks
    83 84 85 86 87 88 89 90 91
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add 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
    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
        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
        }()
    
    
    Expand
  2. Define a private method named setupAnalysisOverlay() to add the distance measurement analysis to the scene.

    ViewController.swift
    Expand
    Use dark colors for code blocks
    83 84 85 86 87 88 89 90 91 92 93 94 95 96
    Add line.Add line.Add line.Add line.Add 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
    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
        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)
        }
    
    Expand
  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
    Expand
    Use dark colors for code blocks
    92 93 94 95 96 97 98 99 100 101 102
    Add line.Add line.Add line.Add line.Add 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
    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
        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
        }
    
    Expand
  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
    Expand
    Use dark colors for code blocks
    98 99 100 101 102 103 104 105 106 107 108 109
    Add line.Add line.Add line.Add line.Add line.Add 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
    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
        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
            }
        }
    
    Expand
  5. Define a private method named clearMeasurement(). This method is used to hide the distance measurement analysis.

    ViewController.swift
    Expand
    Use dark colors for code blocks
    104 105 106 107 108 109 110 111 112 113 114
    Add line.Add line.Add line.Add 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
    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
        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."
        }
    
    Expand
  6. Call clearMeasurement() when the user taps the "Clear" button.

    ViewController.swift
    Expand
    Use dark colors for code blocks
    51 52 53 54 55 56
    Add 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
    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
        @objc
        func clearScene(_ sender: UIBarButtonItem) {
    
            clearMeasurement()
    
        }
    
    Expand
  7. In viewDidLoad(), call the method setupAnalysisOverlay().

    ViewController.swift
    Expand
    Use dark colors for code blocks
    21 22 23 24 25 26 27 28 29
    Add 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
    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
        override func viewDidLoad() {
            super.viewDidLoad()
            setupScene()
    
            setupUI()
    
            setupAnalysisOverlay()
    
        }
    
    Expand

Display the distance measurement analysis with touch events

Touch events determine where to perform and display the distance measurement analysis. A user can long-press and drag to reveal and move the distance measurement analysis' location around the screen.

  1. Extend ViewController to conform to the AGSGeoViewTouchDelegate protocol and include the four long-press geoview touch delegate methods.

    ViewController.swift
    Expand
    Use dark colors for code blocks
    122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    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.Add line.Add line.Add line.Add 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
    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
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
        }
    
        func geoView(_ geoView: AGSGeoView, didMoveLongPressToScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
        }
    
        func geoView(_ geoView: AGSGeoView, didEndLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
        }
    
        func geoViewDidCancelLongPress(_ geoView: AGSGeoView) {
    
        }
    
    }
  2. When the user begins a long-press touch event, call startDistanceMeasurement(point:).

    ViewController.swift
    Expand
    Use dark colors for code blocks
    122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    Add 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
    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
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            startDistanceMeasurement(point: mapPoint)
    
        }
    
        func geoView(_ geoView: AGSGeoView, didMoveLongPressToScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
        }
    
        func geoView(_ geoView: AGSGeoView, didEndLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
        }
    
        func geoViewDidCancelLongPress(_ geoView: AGSGeoView) {
    
        }
    
    }
  3. When the user moves and ends a long-press touch event, call moveDistanceMeasurement(point:).

    ViewController.swift
    Expand
    Use dark colors for code blocks
    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
    Add line.Add 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
    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
    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) {
    
        }
    
    }
  4. When the user cancels the long-press touch event, call clearMeasurement().

    ViewController.swift
    Expand
    Use dark colors for code blocks
    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
    Add 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
    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
    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. In the setupScene() method, assign ViewController to sceneView.touchDelegate.

ViewController.swift
Expand
Use dark colors for code blocks
33 34 35 36 37 38 39 40 41 42 43 44 45
Add 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
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
    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

    }
Expand

Run the app

Press Command + R to run the app.

You should see a scene of hotspots in the Yosemite Valley. Long-press and drag to display and move a distance measurement analysis to evaluate the horizontal, vertical, and direct distances between two park locations.

What's next?

Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials:

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