Learn how to create and display a scene A scene is a collection of layers that are displayed in 3D. It is typically composed of a basemap layer, data layers, and 3D data. Learn more with a basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more and an elevation layer An elevation layer is a layer that defines the ground height or the surface for a scene. Learn more . Set properties of the scene’s camera A camera defines the rendering viewpoint of a 3D scene in a scene view. Learn more to control the 3D perspective.

display a scene

Like a map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more , a scene A scene is a collection of layers that are displayed in 3D. It is typically composed of a basemap layer, data layers, and 3D data. Learn more contains layers A layer is a reference to a collection of geographic data that is used to access and display data. The data for layers are typically provided by the basemap layer service and data services. Learn more of geographic data. It contains a basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more and, optionally, one or more data layers A data layer is a layer that references geographic data from a file or a service and is used to visualize the data in a map or scene. Learn more . To provide a realistic view of the terrain, you can also add elevation layers An elevation layer is a layer that defines the ground height or the surface for a scene. Learn more to define the height of the surface across the scene. The 3D perspective of the scene is controlled by the scene’s camera A camera defines the rendering viewpoint of a 3D scene in a scene view. Learn more , which defines the position of the scene observer in 3D space.

In this tutorial, you create and display a scene A scene is a collection of layers that are displayed in 3D. It is typically composed of a basemap layer, data layers, and 3D data. Learn more using the imagery basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more . The surface of the scene is defined with an elevation layer An elevation layer is a layer that defines the ground height or the surface for a scene. Learn more and the camera A camera defines the rendering viewpoint of a 3D scene in a scene view. Learn more is positioned to display an area of the Santa Monica Mountains in the scene view A scene view is a user interface that displays scene layers and graphics in 3D. It uses a camera to control the visible area of the scene and supports user interactions such as pan, zoom, tilt, and rotate. Learn more .

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.

Set up authentication

To access the secure ArcGIS location services ArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. Learn more used in this tutorial, you must implement API key authentication API key authentication is a type of authentication that uses an API key to authenticate requests to ArcGIS services and secure portal items. Learn more or user authentication User authentication is a type of authentication that allows users with an ArcGIS account to sign into an application and allow it to access ArcGIS content, services, and resources on their behalf. The typical authorization protocol used is OAuth2.0. Learn more using an ArcGIS Location Platform An ArcGIS Location Platform account, formerly known as an ArcGIS Developer account, is an identity associated with an ArcGIS Location Platform subscription. Learn more or an ArcGIS Online An ArcGIS Online account, also known as an ArcGIS Organization account, is an identity associated with an ArcGIS Online subscription. It can be used to access ArcGIS tools and develop applications with ArcGIS location services for an organization. Learn more account.

To complete this tutorial, click on the tab in the switcher below for your authentication type of choice, either API key authentication or User authentication.

Create a new API key access token An access token is an authorization string that provides access to secure ArcGIS content, data, and services. Its capabilities are determined by the privileges it supports. It is obtained by implementing API key authentication, User authentication, or App authentication. Learn more with privileges Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. Learn more to access the secure resources used in this tutorial.

  1. Complete the Create an API key tutorial and create an API key with the following privilege(s) Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. Learn more :

    • Privileges
      • Location services > Basemaps
  2. Copy and paste the API key access token into a safe location. It will be used in a later step.

Develop or Download

You have two options for completing this tutorial:

  1. Option 1: Develop the code or
  2. Option 2: Download the completed solution

Option 1: Develop the code

Create a new Xcode project

To get started, 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, set the following properties:
      • Multiplatform iOS
      • Application App
    • Click Next.
    • In the Choose options for your new project: window, set the following properties:
      • Product Name: <your app name>
      • Organization Identifier: <your organization>
      • Interface: SwiftUI
      • Language: Swift
    • Uncheck all other options.
    • Click Next.
    • Choose a location to store your project. Click Create.
  2. In the Project Navigator, click <your app name>App. In the Editor, right click on the struct name, <your app name>App. Select Refactor > Rename… to rename the struct to MainApp. Click the Rename button in the top right to confirm the new name. This will rename the struct and file in all affected areas. This file and struct will be named MainApp for all tutorials here on out.

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

  4. In the MainApp.swift file, some errors may appear after importing ArcGIS. Resolve the errors by distinguishing the Scene protocol from Scene. To do so, add the SwiftUI prefix to Scene.

    MainApp.swift
    var body: some SwiftUI.Scene {
    WindowGroup {
    ContentView()
    }
    }

Set developer credentials

To allow your app users to access ArcGIS location services ArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. Learn more , use the developer credentials that you created in the Set up authentication step to authenticate requests for resources.

Pass your API Key access token to the ArcGISEnvironment.

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

  2. Implement an initializer in the MainApp struct and set the ArcGISEnvironment.apiKey property with your API key access token.

    MainApp.swift
    import SwiftUI
    import ArcGIS
    @main
    struct MainApp: App {
    init() {
    ArcGISEnvironment.apiKey = APIKey("<#YOUR-ACCESS-TOKEN#>")
    }

Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

Create a scene data model

Create a scene A scene is a collection of layers that are displayed in 3D. It is typically composed of a basemap layer, data layers, and 3D data. Learn more with a standard imagery basemap style. The scene will face the Santa Monica Mountains in California.

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

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

  3. Add a @State property wrapper named scene of type Scene with a default value. Create a scene with an arcGISImageryStandard basemap style and return it.

    ContentView.swift
    15 collapsed lines
    // Copyright 2023 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 SwiftUI
    import ArcGIS
    struct ContentView: View {
    @State private var scene: ArcGIS.Scene = {
    let scene = Scene(basemapStyle: .arcGISImageryStandard)
    return scene
    }()
    var body: some View {
    SceneView(scene: scene)
    }
    }
  4. In the Project Navigator, click MainApp.swift.

Configure the scene

Scenes contain many properties that can be adjusted. Define a Surface on which layers are draped and center the scene on the Santa Monica Mountains.

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

  2. In the editor, create an ArcGISTiledElevationSource and add it to a new Surface.

    ContentView.swift
    19 collapsed lines
    // Copyright 2023 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 SwiftUI
    import ArcGIS
    struct ContentView: View {
    @State private var scene: ArcGIS.Scene = {
    let scene = Scene(basemapStyle: .arcGISImageryStandard)
    // Create an elevation source to show relief in the scene.
    let worldElevationServiceURL = URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
    let elevationSource = ArcGISTiledElevationSource(url: worldElevationServiceURL)
    // Create a Surface with the elevation data.
    let surface = Surface()
    surface.addElevationSource(elevationSource)
    // Add an exaggeration factor to increase the 3D effect of the elevation.
    surface.elevationExaggeration = 2.5
    // Apply the surface to the scene.
    scene.baseSurface = surface
    return scene
    }()
    8 collapsed lines
    var body: some View {
    SceneView(scene: scene)
    }
    }
  3. Set the initial viewpoint of the scene using a Point and a Camera.

    ContentView.swift
    19 collapsed lines
    // Copyright 2023 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 SwiftUI
    import ArcGIS
    struct ContentView: View {
    @State private var scene: ArcGIS.Scene = {
    let scene = Scene(basemapStyle: .arcGISImageryStandard)
    // Create an elevation source to show relief in the scene.
    let worldElevationServiceURL = URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
    let elevationSource = ArcGISTiledElevationSource(url: worldElevationServiceURL)
    // Create a Surface with the elevation data.
    let surface = Surface()
    surface.addElevationSource(elevationSource)
    // Add an exaggeration factor to increase the 3D effect of the elevation.
    surface.elevationExaggeration = 2.5
    // Apply the surface to the scene.
    scene.baseSurface = surface
    // Create a point that defines the observer's (camera) initial location in the scene.
    // The point defines a longitude, latitude, and altitude of the initial camera location.
    let point = Point(x: -118.804, y: 34.027, z: 5330.0, spatialReference: .wgs84)
    // Create a Camera uing the point, the direction the camera should face (heading), and its pitch and roll (rotation and tilt).
    let camera = Camera(location: point, heading: 355.0, pitch: 72.0, roll: 0)
    // Set an initial viewpoint for the scene using the camera and observation point.
    scene.initialViewpoint = Viewpoint(boundingGeometry: point, camera: camera)
    return scene
    }()
    8 collapsed lines
    var body: some View {
    SceneView(scene: scene)
    }
    }

Add a scene view to the UI

A scene view A scene view is a user interface that displays scene layers and graphics in 3D. It uses a camera to control the visible area of the scene and supports user interactions such as pan, zoom, tilt, and rotate. Learn more is a UI component that displays a scene A scene is a collection of layers that are displayed in 3D. It is typically composed of a basemap layer, data layers, and 3D data. Learn more and handles user interactions, including navigating with touch gestures. Add a scene view to the project UI and display the scene that is defined by the Model class.

  1. To the body, add a SceneView initialized with scene. This will create a SceneView with the newly created scene.

    ContentView.swift
    51 collapsed lines
    // Copyright 2023 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 SwiftUI
    import ArcGIS
    struct ContentView: View {
    @State private var scene: ArcGIS.Scene = {
    let scene = Scene(basemapStyle: .arcGISImageryStandard)
    // Create an elevation source to show relief in the scene.
    let worldElevationServiceURL = URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
    let elevationSource = ArcGISTiledElevationSource(url: worldElevationServiceURL)
    // Create a Surface with the elevation data.
    let surface = Surface()
    surface.addElevationSource(elevationSource)
    // Add an exaggeration factor to increase the 3D effect of the elevation.
    surface.elevationExaggeration = 2.5
    // Apply the surface to the scene.
    scene.baseSurface = surface
    // Create a point that defines the observer's (camera) initial location in the scene.
    // The point defines a longitude, latitude, and altitude of the initial camera location.
    let point = Point(x: -118.804, y: 34.027, z: 5330.0, spatialReference: .wgs84)
    // Create a Camera uing the point, the direction the camera should face (heading), and its pitch and roll (rotation and tilt).
    let camera = Camera(location: point, heading: 355.0, pitch: 72.0, roll: 0)
    // Set an initial viewpoint for the scene using the camera and observation point.
    scene.initialViewpoint = Viewpoint(boundingGeometry: point, camera: camera)
    return scene
    }()
    var body: some View {
    SceneView(scene: scene)
    }
    2 collapsed lines
    }

Run the solution

Press Command + R to run the app.

You should see a scene A scene is a collection of layers that are displayed in 3D. It is typically composed of a basemap layer, data layers, and 3D data. Learn more with the imagery basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more centered on the Santa Monica Mountains in California. Drag, pinch, and rotate on the scene view to explore the scene.

Alternatively, you can download the tutorial solution, as follows.

Option 2: Download the solution

  1. Click the Download solution link under Solution and unzip the file to a location on your machine.

  2. Open the .xcodeproj file in Xcode.

Since the downloaded solution does not contain authentication credentials, you must add the developer credentials that you created in the Set up authentication section.

Set developer credentials in the solution

To allow your app users to access ArcGIS location services ArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. Learn more , use the developer credentials that you created in the Set up authentication step to authenticate requests for resources.

Pass your API Key access token to the ArcGISEnvironment.

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

  2. Set the AuthenticationMode to .apiKey.

    MainApp.swift
    // Change the `AuthenticationMode` to `.apiKey` if your application uses API key authentication.
    private var authenticationMode: AuthenticationMode { .apiKey }
  3. Set the apiKey property with your API key access token.

    MainApp.swift
    31 collapsed lines
    // Copyright 2022 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 SwiftUI
    import ArcGIS
    import ArcGISToolkit
    @main
    struct MainApp: App {
    // The authentication mode.
    private enum AuthenticationMode {
    case apiKey
    case user
    }
    // Change the `AuthenticationMode` to `.apiKey` if your application uses API key authentication.
    private var authenticationMode: AuthenticationMode { .apiKey }
    // Please enter an API key access token if your application uses API key authentication.
    private let apiKey = APIKey("<#YOUR-ACCESS-TOKEN#>")
    43 collapsed lines
    // Setup an `Authenticator` with OAuth configuration if your application uses OAuth credentials.
    @ObservedObject var authenticator = Authenticator(
    oAuthUserConfigurations: [
    OAuthUserConfiguration(
    // Please enter OAuth credentials for user authentication.
    portalURL: URL(string: "<#YOUR-PORTAL-URL#>")!,
    clientID: "<#YOUR-CLIENT-ID#>",
    redirectURL: URL(string: "<#YOUR-REDIRECT-URL#>")!
    )
    ]
    )
    func setAuthentication() {
    switch authenticationMode {
    case .apiKey:
    ArcGISEnvironment.apiKey = apiKey
    case .user:
    ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler = authenticator
    }
    }
    init() {
    setAuthentication()
    }
    var body: some SwiftUI.Scene {
    WindowGroup {
    ContentView()
    .authenticator(authenticator)
    .ignoresSafeArea()
    }
    }
    }

Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

Run the solution

Press Command + R to run the app.

You should see a scene A scene is a collection of layers that are displayed in 3D. It is typically composed of a basemap layer, data layers, and 3D data. Learn more with the imagery basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more 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: