Display a scene

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

display a scene

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

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

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

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.

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.

Add a scene view to the UI

A scene view is a UI component that displays a scene. 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 scene view that you will create in the storyboard.

    ViewController.swift
    Use dark colors for code blocks
    14 14 14 14 14 14 14 14 14 14 14 14 14 14 15 16 17 18 19 20 21 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 23 24 25 25 25 26 27 28
    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
    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 scene view to display a scene centered on the Santa Monica Mountains in California. The scene will contain an imagery basemap layer.

  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
    Use dark colors for code blocks
    18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 19 20 21 22 23 24 25 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 27 28 29 30 31 31 31 32 33 34
    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
    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
    Use dark colors for code blocks
    22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 38 38 38 38 38 37 36 36 36 36
    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
        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
    Use dark colors for code blocks
    35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 49 48 47 47 47 47 47 47 46 45 45 45 45
    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
            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
    Use dark colors for code blocks
    48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 49 50 51 52 53 53 53 53 53 52 51 51 51 51
    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
            scene.initialViewpoint = initialViewpoint
    
            sceneView.scene = scene
    
        }
    
    Expand
  6. In the ViewController's viewDidLoad method, call setupScene() once the view has loaded.

    ViewController.swift
    Expand
    Use dark colors for code blocks
    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 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 30 30 30 30 31 32 33 34 35 36 36 36
    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
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupScene()
    
        }
    
    Expand

Set your API key

An API key is required to enable access to services, web maps, and web scenes hosted in ArcGIS Online.

If you haven't already, 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.

  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 API Key.

    AppDelegate.swift
    Expand
    Use dark colors for code blocks
    14 14 14 14 14 14 14 14 14 14 14 14 14 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
    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
    import UIKit
    
    import ArcGIS
    
    
    @main
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        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
        }
    
        // 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 scene with the imagery basemap layer 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.