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 an iOS app and configure it to reference the API.

  1. Open Xcode. In the menu bar, click File > New > Project.

    • In the Choose a template for your new project window, select iOS then App.
    • Click Next.
    • In the Choose a template for your new project window, set the following properties:
      • Product Name: <your app name>
      • Organization Identifier: <your organization>
      • Interface: SwiftUI
      • Language: Swift
    • Click Next.
    • Choose a location to store your project. Click Create.
  2. In the Project Navigatorr, click on the swift file named <your_app_name>App. In the Editor, right click on the struct name, <your_app_name>App. Select Refactor > Rename... and type MainApp. Click the Rename button in the top right to confirm the new name of the file and struct. The MainApp file and struct will be used by all following tutorials.

  3. Add a reference to the API using Swift Package Manager.

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 ContentView.swift.

  2. In the Editor, add a @StateObject property wrapper named scene of type Scene with a default value. Create a ArcGIS.Scene with a arcGISImageryStandard basemap style and return it.

    ContentView.swift
    Expand
    Use dark colors for code blocks
    19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 20 21 22 23 24 24 24 24 24 24 24 24 24 24 24 24 25 26 27 27 27 27 27 28
    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
    struct ContentView: View {
    
        @StateObject private var scene: ArcGIS.Scene = {
            let scene = Scene(basemapStyle: .arcGISImageryStandard)
    
            return scene
        }()
    
    }
  3. Create a new Surface and add a new ArcGISTiledElevationSource to it to define the base surface for the scene. Set the newly created surface as the base surface of the scene.

    An elevation source can define a surface with 3D terrain in a scene. Without an elevation source, the default globe surface is used to display the scene.

    ContentView.swift
    Expand
    Use dark colors for code blocks
    19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 20 21 22 23 24 25 26 27 28 29 30 31 31 31 31 31 32 33 34 34 34 34 34 35
    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
    struct ContentView: View {
    
        @StateObject private var scene: ArcGIS.Scene = {
            let scene = Scene(basemapStyle: .arcGISImageryStandard)
    
            let worldElevationServiceURL = URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
            let elevationSource = ArcGISTiledElevationSource(url: worldElevationServiceURL)
            let surface = Surface()
            surface.addElevationSource(elevationSource)
            surface.elevationExaggeration = 2.5
            scene.baseSurface = surface
    
            return scene
        }()
    
    }
  4. Set the initial viewpoint of the scene using a Point and a Camera.

    The position from which you view the scene is defined by an Camera. The following properties of the camera are used to define an observation point in the scene:

    • 3D location: Latitude, longitude, and altitude
    • Heading: Azimuth of the camera's direction
    • Pitch: Up and down angle
    • Roll: Side-to-side angle
    ContentView.swift
    Expand
    Use dark colors for code blocks
    19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 38 38 38 38 39
    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
    struct ContentView: View {
    
        @StateObject private var scene: ArcGIS.Scene = {
            let scene = Scene(basemapStyle: .arcGISImageryStandard)
    
            let worldElevationServiceURL = URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
            let elevationSource = ArcGISTiledElevationSource(url: worldElevationServiceURL)
            let surface = Surface()
            surface.addElevationSource(elevationSource)
            surface.elevationExaggeration = 2.5
            scene.baseSurface = surface
    
            let point = Point(x: -118.804, y: 34.027, z: 5330.0, spatialReference: .wgs84)
            let camera = Camera(locationPoint: point, heading: 355.0, pitch: 72.0, roll: 0)
            scene.initialViewpoint = Viewpoint(targetExtent: point, camera: camera)
    
            return scene
        }()
    
    }

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.

  1. Add a SceneView initialized with the previously created scene to the ContentView body.

    ContentView.swift
    Expand
    Use dark colors for code blocks
    38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 39 40 41 41 41
    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
        var body: some View {
            SceneView(scene: scene)
        }
    
    Expand

    SceneView conforms to the View protocol.

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 MainApp.swift.

  2. In the Editor, add an import statement to reference the API.

    MainApp.swift
    Use dark colors for code blocks
    15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 16 17 18 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19
    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
    import SwiftUI
    
    import ArcGIS
    
    
  3. Implement an initializer in the MainApp struct. Set the apiKey property on the ArcGISEnvironment with your API Key.

    MainApp.swift
    Expand
    Use dark colors for code blocks
    15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    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
    import SwiftUI
    
    import ArcGIS
    
    @main
    struct MainApp: App {
    
        init() {
            ArcGISEnvironment.apiKey = APIKey("<#your-API-key#>")
        }
    
        var body: some SwiftUI.Scene {
            WindowGroup {
                ContentView()
    
                    .ignoresSafeArea()
    
            }
        }
    
    }
  4. Press <Command+R> to run the app.

    If you are using the Xcode simulator your system must meet these minimum requirements: macOS Big Sur 11.3, Xcode 13.4, iOS 14.0. If you are using a physical device, then refer to the system requirements.

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.