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.

image

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

Steps

Create a new Android Studio project

Use Android Studio to create an app and configure it to reference the API.

  1. Open Android Studio.

    • In the Welcome to Android Studio window, click New Project.

      Or if you already have Android Studio opened, click File > New > New Project in the menu bar.

    • In the New Project window, make sure Phone and Tablet tab is selected, and then select Empty Activity. Click Next.

    • In the next window, set the following options and then click Finish.

      • Name: Display a scene.
      • Package name: Change to com.example.app. Or change to match your organization.
      • Save location: Set to a new folder.
      • Minimum SDK: API 26 ("Oreo"; Android 8.0)
      • Build configuration language: Kotlin DSL (build.gradle.kts)
  2. In the Project tool window, make sure that your current view is Android. These tutorial instructions refer to that view.

  3. From the Project tool window, open Gradle Scripts > build.gradle.kts (Project: Display_a_scene). Replace the contents of the file with the following code:

    build.gradle.kts (Project: Display_a_scene)
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    plugins {
        id("com.android.application") version "8.2.0" apply false
        id("org.jetbrains.kotlin.android") version "1.9.10" apply false
    }
  4. From the Project tool window, open Gradle Scripts > build.gradle.kts (Module :app). Replace the contents of the file with the expanded code below:

    build.gradle.kts (Module: app)
    Expand
    Use dark colors for code blocks
    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
        kotlinOptions {
            jvmTarget = "17"
        }
    
        packaging {
            resources {
                excludes += "/META-INF/DEPENDENCIES"
            }
        }
    
        namespace = "com.example.app"
    }
    
    dependencies {
        implementation("androidx.core:core-ktx:1.12.0")
        implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
        implementation("androidx.activity:activity-compose:1.8.2")
        // Jetpack Compose Bill of Materials
        implementation(platform("androidx.compose:compose-bom:2023.10.01"))
        // Jetpack Compose dependencies
        implementation("androidx.compose.ui:ui")
        implementation("androidx.compose.material3:material3")
        // ArcGIS Map Kotlin SDK dependencies
        implementation("com.esri:arcgis-maps-kotlin:200.4.0")
        // Toolkit dependencies
        implementation(platform("com.esri:arcgis-maps-kotlin-toolkit-bom:200.4.0"))
        implementation("com.esri:arcgis-maps-kotlin-toolkit-geoview-compose")
    }
  5. From the Project tool window, open Gradle Scripts > settings.gradle.kts. Replace the contents of the file with the expanded code below:

    settings.gradle.kts (Project Settings)
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    pluginManagement {
        repositories {
            google()
            mavenCentral()
            gradlePluginPortal()
        }
    }
    
    dependencyResolutionManagement {
        @Suppress("UnstableApiUsage")
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        @Suppress("UnstableApiUsage")
        repositories {
            google()
    
    Expand
  6. Sync the Gradle changes. Click the Sync now prompt or click the refresh icon (Sync Project with Gradle Files) in the toolbar. This may take several minutes.

  7. From the Project tool window, open app > manifests > AndroidManifest.xml. Update the Android manifest to allow internet access.

    Insert these new elements within the manifest element. Do not alter or remove any other statements.

    Depending on what ArcGIS functionality you add in future tutorials, it is likely you will need to add additional permissions to your manifest.

    AndroidManifest.xml
    Expand
    Use dark colors for code blocks
    2 2 3 4 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
    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
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    
        <uses-permission android:name="android.permission.INTERNET" />
    
    Expand

Create a scene

  1. From the Project tool window, right click on app > kotlin+java > com.example.app, select New > package from the list. Enter com.example.app.screens as the package name. Hit Enter on your keyboard. This step creates a new package that will contain all the UI files.

  2. Right click on the screens package you just created, select New > Kotlin Class/File from the list. In the pop-up window, select File and enter MainScreen as the file name. Hit Enter on your keyboard.

  3. In MainScreen.kt, delete any lines of code that were inserted automatically by Android Studio. Then add the following OptIn annotation, package name, and imports.

    MainScreen.kt
    Use dark colors for code blocks
    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
    @file:OptIn(ExperimentalMaterial3Api::class)
    
    package com.example.app.screens
    
    import androidx.compose.foundation.layout.fillMaxSize
    import androidx.compose.foundation.layout.padding
    import androidx.compose.material3.ExperimentalMaterial3Api
    import androidx.compose.material3.Scaffold
    import androidx.compose.material3.Text
    import androidx.compose.material3.TopAppBar
    import androidx.compose.runtime.Composable
    import androidx.compose.runtime.remember
    import androidx.compose.ui.Modifier
    import androidx.compose.ui.res.stringResource
    import com.arcgismaps.geometry.Point
    import com.arcgismaps.geometry.SpatialReference
    import com.arcgismaps.mapping.ArcGISScene
    import com.arcgismaps.mapping.ArcGISTiledElevationSource
    import com.arcgismaps.mapping.BasemapStyle
    import com.arcgismaps.mapping.Surface
    import com.arcgismaps.mapping.Viewpoint
    import com.arcgismaps.mapping.view.Camera
    import com.arcgismaps.toolkit.geoviewcompose.SceneView
    import com.example.app.R
    
    
  4. You will start by creating a function named createScene().

    Inside that function, you will create an ArcGISScene, assign a base surface to it, and use the top-level composable function remember to retain state across recompositions.

    Then you will create a camera location and a Camera, use them to create a Viewpoint, and then assign the view point to the initialViewpoint property of the ArcGISScene.

    1. Create a top-level function named createScene() that returns an ArcGISScene.

      MainScreen.kt
      Expand
      Use dark colors for code blocks
      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
      fun createScene(): ArcGISScene {
      
      }
    2. Create a new ArcGISTiledElevationSource. Then create a Surface and, inside the apply block for Surface, add the elevation source to the elevationSources property, and set the elevationExaggeration property to 2.5f, which increases the 3D effect of the elevation.

      MainScreen.kt
      Expand
      Use dark colors for code blocks
      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
      fun createScene(): ArcGISScene {
      
          // add base surface for elevation data
          val elevationSource = ArcGISTiledElevationSource("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")
          val surface = Surface().apply {
              elevationSources.add(elevationSource)
              // add an exaggeration factor to increase the 3D effect of the elevation.
              elevationExaggeration = 2.5f
          }
      
      }
    3. Create a Point for the camera and assign it to the variable cameraLocation. Then create a Camera, passing the camera ocation and values for the camera's heading, pitch, and roll.

      MainScreen.kt
      Expand
      Use dark colors for code blocks
      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
      fun createScene(): ArcGISScene {
      
          // add base surface for elevation data
          val elevationSource = ArcGISTiledElevationSource("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")
          val surface = Surface().apply {
              elevationSources.add(elevationSource)
              // add an exaggeration factor to increase the 3D effect of the elevation.
              elevationExaggeration = 2.5f
          }
      
          val cameraLocation = Point(
              x = -118.794,
              y = 33.909,
              z = 5330.0,
              spatialReference = SpatialReference.wgs84()
          )
      
          val camera = Camera(
              locationPoint = cameraLocation,
              heading = 355.0,
              pitch = 72.0,
              roll = 0.0
          )
      
      }
    4. Create an ArcGISScene with a BasemapStyle.ArcGISImagery. Then call apply() on the new ArcGISScene. The createScene() function returns this ArcGISScene.

      MainScreen.kt
      Expand
      Use dark colors for code blocks
      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
      fun createScene(): ArcGISScene {
      
          // add base surface for elevation data
          val elevationSource = ArcGISTiledElevationSource("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")
          val surface = Surface().apply {
              elevationSources.add(elevationSource)
              // add an exaggeration factor to increase the 3D effect of the elevation.
              elevationExaggeration = 2.5f
          }
      
          val cameraLocation = Point(
              x = -118.794,
              y = 33.909,
              z = 5330.0,
              spatialReference = SpatialReference.wgs84()
          )
      
          val camera = Camera(
              locationPoint = cameraLocation,
              heading = 355.0,
              pitch = 72.0,
              roll = 0.0
          )
      
          return ArcGISScene(BasemapStyle.ArcGISImagery).apply {
      
          }
      
      }
    5. In the apply block, set the baseSurface property of the ArcGISScene to surface.

      MainScreen.kt
      Expand
      Use dark colors for code blocks
      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
      fun createScene(): ArcGISScene {
      
          // add base surface for elevation data
          val elevationSource = ArcGISTiledElevationSource("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")
          val surface = Surface().apply {
              elevationSources.add(elevationSource)
              // add an exaggeration factor to increase the 3D effect of the elevation.
              elevationExaggeration = 2.5f
          }
      
          val cameraLocation = Point(
              x = -118.794,
              y = 33.909,
              z = 5330.0,
              spatialReference = SpatialReference.wgs84()
          )
      
          val camera = Camera(
              locationPoint = cameraLocation,
              heading = 355.0,
              pitch = 72.0,
              roll = 0.0
          )
      
          return ArcGISScene(BasemapStyle.ArcGISImagery).apply {
      
              baseSurface = surface
      
          }
      
      }
    6. Create a Viewpoint using cameraLocation and camera and set it as the initial viewpoint for the scene.

      MainScreen.kt
      Expand
      Use dark colors for code blocks
      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
      fun createScene(): ArcGISScene {
      
          // add base surface for elevation data
          val elevationSource = ArcGISTiledElevationSource("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")
          val surface = Surface().apply {
              elevationSources.add(elevationSource)
              // add an exaggeration factor to increase the 3D effect of the elevation.
              elevationExaggeration = 2.5f
          }
      
          val cameraLocation = Point(
              x = -118.794,
              y = 33.909,
              z = 5330.0,
              spatialReference = SpatialReference.wgs84()
          )
      
          val camera = Camera(
              locationPoint = cameraLocation,
              heading = 355.0,
              pitch = 72.0,
              roll = 0.0
          )
      
          return ArcGISScene(BasemapStyle.ArcGISImagery).apply {
      
              baseSurface = surface
      
              initialViewpoint = Viewpoint(cameraLocation, camera)
      
          }
      
      }

Create a MainScreen to hold the scene

  1. In MainScreen.kt, create a composable function named MainScreen, which will call SceneView.

    MainScreen.kt
    Expand
    Use dark colors for code blocks
    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
    @Composable
    fun MainScreen() {
    
    }
    
    Expand
  2. Add a remember block and call createScene() inside it. Assign remember to a local variable named scene.

    MainScreen.kt
    Expand
    Use dark colors for code blocks
    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
    @Composable
    fun MainScreen() {
    
        val scene = remember {
            createScene()
        }
    
    }
    
    Expand
  3. You will now call several composable functions from Android Jetpack Compose. Call Scaffold and pass a TopAppBar with a Text that contains the app name (R.string.app_name).

    MainScreen.kt
    Expand
    Use dark colors for code blocks
    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
    @Composable
    fun MainScreen() {
    
        val scene = remember {
            createScene()
        }
    
        Scaffold(
            topBar = { TopAppBar(title = { Text(text = stringResource(id = R.string.app_name)) }) }
        ) {
    
        }
    
    }
    
    Expand
  4. In the trailing lambda for Scaffold, call the SceneView composable defined in the ArcGIS Maps SDK for Kotlin Toolkit. Pass a Modifier that has maximum size and default padding. And pass scene as the arcGISScene parameter

    MainScreen.kt
    Expand
    Use dark colors for code blocks
    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
    @Composable
    fun MainScreen() {
    
        val scene = remember {
            createScene()
        }
    
        Scaffold(
            topBar = { TopAppBar(title = { Text(text = stringResource(id = R.string.app_name)) }) }
        ) {
    
            SceneView(
                modifier = Modifier.fillMaxSize().padding(it),
                arcGISScene = scene
            )
    
        }
    
    }
    
    Expand

Call MainScreen inside MainActivity class

  1. Open the app > kotlin+java > com.example.app > MainActivity.kt. Delete all lines of code except for the package declaration (the first line) and the MainActivity class definition.

    MainActivity.kt
    Use dark colors for code blocksCopy
    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
    package com.example.app
    
    class MainActivity : ComponentActivity() {
    
    }
  2. Add import statements to MainActivity.kt.

    MainActivity.kt
    Use dark colors for code blocks
    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
    package com.example.app
    
    import android.os.Bundle
    import androidx.activity.ComponentActivity
    import androidx.activity.compose.setContent
    import com.arcgismaps.ApiKey
    import com.arcgismaps.ArcGISEnvironment
    import com.example.app.screens.MainScreen
    import com.example.app.ui.theme.DisplayASceneTheme
    
    class MainActivity : ComponentActivity() {
    
    }
  3. In the setContent block of the onCreate() lifecylce function, you will call the composable function MainScreen, with default theming applied. To do this, add onCreate() with the following code.

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    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
    class MainActivity : ComponentActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            setContent {
                DisplayASceneTheme {
                    MainScreen()
                }
            }
        }
    
    }

Set your API key

An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.

  1. In the MainActivity class, create the setApiKey() method, where you set the ArcGISEnvironment.apiKey​ property by calling ApiKey.create() and passing your API key as a string. Don't forget the quotes.

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    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
    class MainActivity : ComponentActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            setContent {
                DisplayASceneTheme {
                    MainScreen()
                }
            }
        }
    
        private fun setApiKey() {
    
            ArcGISEnvironment.apiKey = ApiKey.create("YOUR_API_KEY")
    
        }
    
    }
  2. Call setApiKey() in the onCreate() lifecycle method, before setContent {}.

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    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
    class MainActivity : ComponentActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            setApiKey()
    
            setContent {
                DisplayASceneTheme {
                    MainScreen()
                }
            }
        }
    
        private fun setApiKey() {
    
            ArcGISEnvironment.apiKey = ApiKey.create("YOUR_API_KEY")
    
        }
    
    }

Run your app

  1. Click Run > Run > app to run the app.

You should see a scene with the imagery basemap layer centered on the Santa Monica Mountains in California. Pinch, drag, and double-tap 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.