Display a scene

Learn how to create and display a with a and an . Set properties of the scene's to control the 3D perspective.

display a scene

Like a , a contains of geographic data. It contains a and, optionally, one or more . To provide a realistic view of the terrain, you can also add to define the height of the surface across the scene. The 3D perspective of the scene is controlled by the scene's , which defines the position of the scene observer in 3D space.

In this tutorial, you create and display a using the imagery . The surface of the scene is defined with an and the is positioned to display an area of the Santa Monica Mountains in the .

The scene and code will be used as the starting point for other 3D tutorials.

Prerequisites

Before starting this tutorial:

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

  2. Your system meets the system requirements.

Steps

Create a new Xcode project

Use Xcode to create a single view iOS app and configure it to reference the API.

  1. Open Xcode. In the menu bar, click File > New > Project > iOS > Single View App > Next.

    • In the Choose options window, set the following properties:
      • Product Name: <your app name>
      • Language: Swift
      • User interface: Storyboard
      • Organization Identifier: <your organization>
    • Uncheck all other options.
    • Click Next > Create.
  2. Add a reference to the API by following the instructions in Get the API - Configure a project.

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

Add a scene view to the UI

A is a UI component that displays a . It also handles user interactions with the scene, including navigating with touch gestures. Use Xcode and the storyboard editor to add a scene view to the UI and connect it to the view controller source code. Set the scene view size to fill the entire device display.

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

  2. In the editor, add an import statement to reference the API and add an @IBOutlet named sceneView and of type AGSSceneView. This will provide a reference to the that you will create in the storyboard.

    ViewController.swift
    15 16 17 18 19 20 21 22 23 24 25 26 27 28
    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
    import UIKit
    
    import ArcGIS
    
    class ViewController: UIViewController {
    
        @IBOutlet var sceneView: AGSSceneView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
        }
    
    }
  3. In the Project Navigator, click Main.storyboard to open the storyboard editor.

  4. In the menu, click View > Show Library to display the object library.

  5. In the object library browser:

    • Type uiview or scroll down to find View.
    • Drag and drop a new view on to the storyboard's main view.
  6. At the bottom right of the storyboard editor, click Add New Constraints. In the panel:

    • Type 0 for the top, right, bottom, and left constraints.
    • Click Add 4 Constraints.

    The new view expands to fill the display.

  7. In the menu, click View > Inspectors > Show Identity Inspector. In the Inspectors panel, set Custom Class > Class to AGSSceneView.

    This sets the type of the new view to AGSSceneView.

  8. In the storyboard editor, right-click on the yellow View Controller icon to display the Connections panel. Drag the sceneView outlet connector to the new AGSSceneView view on the storyboard.

    This connects the AGSSceneView in the storyboard to the sceneView outlet created earlier in the ViewController class.

Add a scene

Use the to display a centered on the Santa Monica Mountains in California. The scene will contain an imagery .

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

  2. In the editor, define a private method named setupScene(). In setupScene() create an AGSScene.

    ViewController.swift
    Expand
    19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    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
    class ViewController: UIViewController {
    
        @IBOutlet var sceneView: AGSSceneView!
    
        private func setupScene() {
    
            let scene = AGSScene(basemapStyle: .arcGISImageryStandard)
    
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
        }
    
    }
  3. Create a new AGSSurface and add a new AGSArcGISTiledElevationSource to it to define the base surface for the scene. Set the newly created surface as the base surface of the scene.

    ViewController.swift
    Expand
    23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    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
        private func setupScene() {
    
            let scene = AGSScene(basemapStyle: .arcGISImageryStandard)
    
            let elevationSurface: AGSSurface = {
                let elevationServiceURL = URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
                let elevationSource = AGSArcGISTiledElevationSource(url: elevationServiceURL)
                let surface = AGSSurface()
                surface.elevationSources.append(elevationSource)
                surface.elevationExaggeration = 2.5
                return surface
            }()
    
            scene.baseSurface = elevationSurface
    
        }
    
    Expand
  4. Set the initial viewpoint of the sceneView using an AGSPoint and an AGSCamera.

    ViewController.swift
    Expand
    36 37 38 39 40 41 42 43 44 45 46 47 48 49
    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
            scene.baseSurface = elevationSurface
    
            let initialViewpoint = AGSViewpoint(
                center: AGSPoint(x: -118.805, y: 34.027, spatialReference: .wgs84()),
                scale: 10000,
                camera: AGSCamera(
                    location: AGSPoint(x: -118.804, y: 33.909, z: 5330.0, spatialReference: .wgs84()),
                    heading: 355.0,
                    pitch: 72.0,
                    roll: 0.0
                )
            )
    
            scene.initialViewpoint = initialViewpoint
    
    Expand
  5. Set the scene property of the sceneView outlet to the new AGSScene.

    ViewController.swift
    Expand
    49 50 51 52 53
    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
            scene.initialViewpoint = initialViewpoint
    
            sceneView.scene = scene
    
        }
    
    Expand
  6. In the ViewController's viewDidLoad method, call setupScene() once the view has loaded.

    ViewController.swift
    Expand
    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
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupScene()
    
        }
    
    Expand

Get an access token

You need an to use the used in this tutorial.

  1. Go to the Create an API key tutorial to obtain an using your or account.

  2. Ensure that the following is enabled: Location services > Basemaps > Basemap styles service.

  3. Copy the access token as it will be used in the next step.

To learn more about other ways to get an access token, go to Types of authentication.

Set your API key

  1. In the Project Navigator, click AppDelegate.swift.

  2. In the editor, add an import statement to reference the API and in the AppDelegate's application(_:didFinishLaunchingWithOptions:) method, set the apiKey property on the AGSArcGISRuntimeEnvironment with your .

    AppDelegate.swift
    Expand
    15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    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
    import UIKit
    
    import ArcGIS
    
    
    @main
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        func application(_ application: UIApplication,
                         didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
            AGSArcGISRuntimeEnvironment.apiKey = "YOUR_ACCESS_TOKEN"
    
            return true
        }
    
        // MARK: UISceneSession Lifecycle
    
        func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
            // Called when a new scene session is being created.
            // Use this method to select a configuration to create the new scene with.
            return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
        }
    }
  3. Press Command + R to run the app.

You should see a with the imagery centered on the Santa Monica Mountains in California. Drag, pinch, and rotate on the scene view to explore the scene.

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.

You can no longer sign into this site. Go to your ArcGIS portal or the ArcGIS Location Platform dashboard to perform management tasks.

Your ArcGIS portal

Create, manage, and access API keys and OAuth 2.0 developer credentials, hosted layers, and data services.

Your ArcGIS Location Platform dashboard

Manage billing, monitor service usage, and access additional resources.

Learn more about these changes in the What's new in Esri Developers June 2024 blog post.

Close