3D terrain view of Yosemite Valley with red and green viewshed overlays and a distance slider.

Viewshed analysis determines the visibility of terrain, buildings, and other 3D objects from an observer’s location within a scene (using a specified field of view). The result indicates which areas are visible and which are obstructed when viewed from the observer’s perspective.

In this tutorial, you will perform and display a viewshed analysis in a web scene. Your viewshed analysis will show visibility (visible or obstructed) and can be used to determine which hotspots in the Yosemite Valley are visible from a specified observer’s perspective.

Prerequisites

Before starting this tutorial, you need the following:

  1. An ArcGIS Location Platform or ArcGIS Online account.

  2. A development and deployment environment that meets the system requirements.

  3. An IDE for Android development in Kotlin.

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

Open an Android Studio project

  1. Open the project you created by completing the Display a scene tutorial.

  2. Continue with the following instructions to perform and display a viewshed analysis in a 3D scene.

  3. Modify the old project for use in this new tutorial.

  4. In libs.versions.toml, add a [libraries] entry for the dependency. In the module-level build.gradle.kts (app), add the dependency for the lifecycle view model.

    19 collapsed lines
    [versions]
    arcgisMapsKotlin = "300.1.0"
    # Version numbers added by Android Studio New Project Wizard
    agp = "9.2.1"
    kotlin = "2.3.21"
    coreKtx = "1.18.0"
    junit = "4.13.2"
    junitVersion = "1.3.0"
    espressoCore = "3.7.0"
    lifecycleRuntimeKtx = "2.9.2"
    activityCompose = "1.13.0"
    composeBom = "2026.04.01"
    # Other version numbers
    compileSdk = "37"
    minSdk = "28"
    targetSdk = "37"
    [libraries]
    arcgis-maps-kotlin = { group = "com.esri", name = "arcgis-maps-kotlin", version.ref = "arcgisMapsKotlin" }
    arcgis-maps-kotlin-toolkit-bom = { group = "com.esri", name = "arcgis-maps-kotlin-toolkit-bom", version.ref = "arcgisMapsKotlin" }
    arcgis-maps-kotlin-toolkit-geoview-compose = { group = "com.esri", name = "arcgis-maps-kotlin-toolkit-geoview-compose" }
    arcgis-maps-kotlin-toolkit-authentication = { group = "com.esri", name = "arcgis-maps-kotlin-toolkit-authentication" }
    androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
    junit = { group = "junit", name = "junit", version.ref = "junit" }
    androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
    androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
    androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
    androidx-lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "lifecycleRuntimeKtx" }
    14 collapsed lines
    androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
    androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
    androidx-ui = { group = "androidx.compose.ui", name = "ui" }
    androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
    androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
    androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
    androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
    androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
    androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
    [plugins]
    android-application = { id = "com.android.application", version.ref = "agp" }
    kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
    • Click File > Sync Project with Gradle files.

    Add imports

    Modify import statements to reference the packages and classes required for this tutorial.

    MainScreen.kt
    @file:OptIn(ExperimentalMaterial3Api::class)
    package com.example.app.screens
    import android.util.Log
    import androidx.compose.foundation.layout.Arrangement
    import androidx.compose.foundation.layout.Column
    import androidx.compose.foundation.layout.Row
    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.Slider
    import androidx.compose.material3.Text
    import androidx.compose.material3.TextButton
    import androidx.compose.material3.TopAppBar
    import androidx.compose.runtime.Composable
    import androidx.compose.runtime.getValue
    import androidx.compose.runtime.mutableFloatStateOf
    import androidx.compose.runtime.remember
    import androidx.compose.runtime.setValue
    import androidx.compose.ui.Alignment
    import androidx.compose.ui.Modifier
    import androidx.compose.ui.res.stringResource
    import androidx.compose.ui.unit.dp
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import androidx.lifecycle.viewmodel.compose.viewModel
    import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
    import com.arcgismaps.geometry.Point
    import com.arcgismaps.mapping.ArcGISScene
    import com.arcgismaps.mapping.PortalItem
    import com.arcgismaps.mapping.view.AnalysisOverlay
    import com.arcgismaps.mapping.view.ScreenCoordinate
    import com.arcgismaps.portal.Portal
    import com.arcgismaps.toolkit.geoviewcompose.SceneView
    import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
    import com.example.app.R
    import kotlinx.coroutines.launch
    import kotlin.math.roundToInt

      Get the web scene item ID

      You can use ArcGIS toolsTools, also known as developer tools, are ArcGIS software applications such as portal and ArcGIS Pro that developers can use to prepare content and data for custom applications they are building. Learn more to create and view web scenesA web scene is a scene stored as a JSON object that defines properties such as the basemap layer, data layers, layer styles, and pop-up styles. Its JSON structure is defined by the web scene specification. Learn more . Use the Scene ViewerScene Viewer(™) is a browser-based mapping tool that can view, create, and save web scenes. Learn more to identify the web scene item IDAn item ID is a unique identifier representing a single item stored, managed, and accessed in a portal, such as a web map, hosted layer, or file. Learn more . This item ID will be used later in the tutorial.

      1. Go to the Yosemite Valley Hotspots web scene in the Scene Viewer in ArcGIS OnlineArcGIS Online is a GIS mapping, analytics, data hosting, and content management software as a service (SaaS) product. It includes applications, tools, APIs, and location services for users and developers. It is subscription-based and requires an ArcGIS Online account. Learn more . This web scene displays terrain and hotspots in the Yosemite Valley.
      2. Make a note of the item IDAn item ID is a unique identifier representing a single item stored, managed, and accessed in a portal, such as a web map, hosted layer, or file. Learn more at the end of the browser’s URL. The item ID should be 7558ee942b2547019f66885c44d4f0b1.

      Create a view model

      Modern app architecture uses a scene view model to hold the business logic and the mutual state of your app.

      1. In Android Studio: in the Android view, open app > kotlin+java > com.example.app > screens > MainScreen.kt

        Create a scene view model that extends ViewModel.

        MainScreen.kt
        42 collapsed lines
        @file:OptIn(ExperimentalMaterial3Api::class)
        package com.example.app.screens
        import android.util.Log
        import androidx.compose.foundation.layout.Arrangement
        import androidx.compose.foundation.layout.Column
        import androidx.compose.foundation.layout.Row
        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.Slider
        import androidx.compose.material3.Text
        import androidx.compose.material3.TextButton
        import androidx.compose.material3.TopAppBar
        import androidx.compose.runtime.Composable
        import androidx.compose.runtime.getValue
        import androidx.compose.runtime.mutableFloatStateOf
        import androidx.compose.runtime.remember
        import androidx.compose.runtime.setValue
        import androidx.compose.ui.Alignment
        import androidx.compose.ui.Modifier
        import androidx.compose.ui.res.stringResource
        import androidx.compose.ui.unit.dp
        import androidx.lifecycle.ViewModel
        import androidx.lifecycle.viewModelScope
        import androidx.lifecycle.viewmodel.compose.viewModel
        import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
        import com.arcgismaps.geometry.Point
        import com.arcgismaps.mapping.ArcGISScene
        import com.arcgismaps.mapping.PortalItem
        import com.arcgismaps.mapping.view.AnalysisOverlay
        import com.arcgismaps.mapping.view.ScreenCoordinate
        import com.arcgismaps.portal.Portal
        import com.arcgismaps.toolkit.geoviewcompose.SceneView
        import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
        import com.example.app.R
        import kotlinx.coroutines.launch
        import kotlin.math.roundToInt
        class SceneViewModel : ViewModel() {
        }
        5 collapsed lines
        @Composable
        fun MainScreen() {
        }
        • In the MainScreen composable, delete the entire function body. Leave just the function declaration as shown below. Then delete the top-level code for creating a scene that is part of the Display a scene tutorial. In this tutorial, you will create the scene within the view model.

          MainScreen.kt
          109 collapsed lines
          @file:OptIn(ExperimentalMaterial3Api::class)
          package com.example.app.screens
          import android.util.Log
          import androidx.compose.foundation.layout.Arrangement
          import androidx.compose.foundation.layout.Column
          import androidx.compose.foundation.layout.Row
          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.Slider
          import androidx.compose.material3.Text
          import androidx.compose.material3.TextButton
          import androidx.compose.material3.TopAppBar
          import androidx.compose.runtime.Composable
          import androidx.compose.runtime.getValue
          import androidx.compose.runtime.mutableFloatStateOf
          import androidx.compose.runtime.remember
          import androidx.compose.runtime.setValue
          import androidx.compose.ui.Alignment
          import androidx.compose.ui.Modifier
          import androidx.compose.ui.res.stringResource
          import androidx.compose.ui.unit.dp
          import androidx.lifecycle.ViewModel
          import androidx.lifecycle.viewModelScope
          import androidx.lifecycle.viewmodel.compose.viewModel
          import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
          import com.arcgismaps.geometry.Point
          import com.arcgismaps.mapping.ArcGISScene
          import com.arcgismaps.mapping.PortalItem
          import com.arcgismaps.mapping.view.AnalysisOverlay
          import com.arcgismaps.mapping.view.ScreenCoordinate
          import com.arcgismaps.portal.Portal
          import com.arcgismaps.toolkit.geoviewcompose.SceneView
          import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
          import com.example.app.R
          import kotlinx.coroutines.launch
          import kotlin.math.roundToInt
          class SceneViewModel : ViewModel() {
          private val portal = Portal(
          url = "https://www.arcgis.com",
          connection = Portal.Connection.Anonymous
          )
          private val portalItem = PortalItem(
          portal = portal,
          itemId = "7558ee942b2547019f66885c44d4f0b1"
          )
          var scene = ArcGISScene(portalItem)
          val sceneViewProxy = SceneViewProxy()
          // The analysis overlay to be added to the scene.
          val analysisOverlay = AnalysisOverlay()
          var viewshed = ExploratoryLocationViewshed(
          location = Point(x = 0.0, y = 0.0),
          heading = 0.0,
          pitch = 90.0,
          horizontalAngle = 360.0,
          verticalAngle = 180.0,
          minDistance = 10.0,
          maxDistance = 12_000.0
          )
          init {
          viewshed.isVisible = false
          analysisOverlay.analyses.add(viewshed)
          viewModelScope.launch {
          scene.load().getOrElse { logError(it) }
          }
          }
          private fun setViewshedLocation(point: Point) {
          viewshed.location = point
          viewshed.isVisible = true
          }
          fun hideViewshed() {
          viewshed.isVisible = false
          }
          fun setViewshedDistance(maxDistance: Double) {
          viewshed.maxDistance = maxDistance
          }
          fun handleOnLongPress(screenCoordinate: ScreenCoordinate) {
          viewModelScope.launch {
          val scenePoint = sceneViewProxy.screenToLocation(screenCoordinate).getOrElse { error ->
          return@launch logError(error)
          }
          setViewshedLocation(scenePoint)
          }
          }
          private fun logError(error: Throwable) {
          Log.e(this.javaClass.simpleName, error.message.toString(), error.cause)
          }
          }
          @Composable
          fun MainScreen() {
          }
            MainScreen.kt
            25 collapsed lines
            @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
            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)
            }
            }
            20 collapsed lines
            @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
            )
            }
            }
            • In the view model, create an ArcGISScene from a PortalItem. Then create a SceneViewProxy.

              MainScreen.kt
              44 collapsed lines
              @file:OptIn(ExperimentalMaterial3Api::class)
              package com.example.app.screens
              import android.util.Log
              import androidx.compose.foundation.layout.Arrangement
              import androidx.compose.foundation.layout.Column
              import androidx.compose.foundation.layout.Row
              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.Slider
              import androidx.compose.material3.Text
              import androidx.compose.material3.TextButton
              import androidx.compose.material3.TopAppBar
              import androidx.compose.runtime.Composable
              import androidx.compose.runtime.getValue
              import androidx.compose.runtime.mutableFloatStateOf
              import androidx.compose.runtime.remember
              import androidx.compose.runtime.setValue
              import androidx.compose.ui.Alignment
              import androidx.compose.ui.Modifier
              import androidx.compose.ui.res.stringResource
              import androidx.compose.ui.unit.dp
              import androidx.lifecycle.ViewModel
              import androidx.lifecycle.viewModelScope
              import androidx.lifecycle.viewmodel.compose.viewModel
              import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
              import com.arcgismaps.geometry.Point
              import com.arcgismaps.mapping.ArcGISScene
              import com.arcgismaps.mapping.PortalItem
              import com.arcgismaps.mapping.view.AnalysisOverlay
              import com.arcgismaps.mapping.view.ScreenCoordinate
              import com.arcgismaps.portal.Portal
              import com.arcgismaps.toolkit.geoviewcompose.SceneView
              import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
              import com.example.app.R
              import kotlinx.coroutines.launch
              import kotlin.math.roundToInt
              class SceneViewModel : ViewModel() {
              private val portal = Portal(
              url = "https://www.arcgis.com",
              connection = Portal.Connection.Anonymous
              )
              private val portalItem = PortalItem(
              portal = portal,
              itemId = "7558ee942b2547019f66885c44d4f0b1"
              )
              var scene = ArcGISScene(portalItem)
              val sceneViewProxy = SceneViewProxy()
              8 collapsed lines
              }
              @Composable
              fun MainScreen() {
              }

              Create a viewshed analysis

              Visual analyses are used to help you make sense of complex 3D data contained by a scene. Use an ExploratoryLocationViewshed to perform and display a viewshedA viewshed represents the locations visible from one or more specified points or lines. Learn more analysis using a 3D point to define the observer’s location.

              1. In the view model, create an AnalysisOverlay named analysisOverlay to contain and display the viewshed analyses.

                SceneViewModel class
                58 collapsed lines
                @file:OptIn(ExperimentalMaterial3Api::class)
                package com.example.app.screens
                import android.util.Log
                import androidx.compose.foundation.layout.Arrangement
                import androidx.compose.foundation.layout.Column
                import androidx.compose.foundation.layout.Row
                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.Slider
                import androidx.compose.material3.Text
                import androidx.compose.material3.TextButton
                import androidx.compose.material3.TopAppBar
                import androidx.compose.runtime.Composable
                import androidx.compose.runtime.getValue
                import androidx.compose.runtime.mutableFloatStateOf
                import androidx.compose.runtime.remember
                import androidx.compose.runtime.setValue
                import androidx.compose.ui.Alignment
                import androidx.compose.ui.Modifier
                import androidx.compose.ui.res.stringResource
                import androidx.compose.ui.unit.dp
                import androidx.lifecycle.ViewModel
                import androidx.lifecycle.viewModelScope
                import androidx.lifecycle.viewmodel.compose.viewModel
                import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
                import com.arcgismaps.geometry.Point
                import com.arcgismaps.mapping.ArcGISScene
                import com.arcgismaps.mapping.PortalItem
                import com.arcgismaps.mapping.view.AnalysisOverlay
                import com.arcgismaps.mapping.view.ScreenCoordinate
                import com.arcgismaps.portal.Portal
                import com.arcgismaps.toolkit.geoviewcompose.SceneView
                import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
                import com.example.app.R
                import kotlinx.coroutines.launch
                import kotlin.math.roundToInt
                class SceneViewModel : ViewModel() {
                private val portal = Portal(
                url = "https://www.arcgis.com",
                connection = Portal.Connection.Anonymous
                )
                private val portalItem = PortalItem(
                portal = portal,
                itemId = "7558ee942b2547019f66885c44d4f0b1"
                )
                var scene = ArcGISScene(portalItem)
                val sceneViewProxy = SceneViewProxy()
                // The analysis overlay to be added to the scene.
                val analysisOverlay = AnalysisOverlay()
                8 collapsed lines
                }
                @Composable
                fun MainScreen() {
                }
                • Create an ExploratoryLocationViewshed named viewshed.

                  SceneViewModel class
                  61 collapsed lines
                  @file:OptIn(ExperimentalMaterial3Api::class)
                  package com.example.app.screens
                  import android.util.Log
                  import androidx.compose.foundation.layout.Arrangement
                  import androidx.compose.foundation.layout.Column
                  import androidx.compose.foundation.layout.Row
                  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.Slider
                  import androidx.compose.material3.Text
                  import androidx.compose.material3.TextButton
                  import androidx.compose.material3.TopAppBar
                  import androidx.compose.runtime.Composable
                  import androidx.compose.runtime.getValue
                  import androidx.compose.runtime.mutableFloatStateOf
                  import androidx.compose.runtime.remember
                  import androidx.compose.runtime.setValue
                  import androidx.compose.ui.Alignment
                  import androidx.compose.ui.Modifier
                  import androidx.compose.ui.res.stringResource
                  import androidx.compose.ui.unit.dp
                  import androidx.lifecycle.ViewModel
                  import androidx.lifecycle.viewModelScope
                  import androidx.lifecycle.viewmodel.compose.viewModel
                  import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
                  import com.arcgismaps.geometry.Point
                  import com.arcgismaps.mapping.ArcGISScene
                  import com.arcgismaps.mapping.PortalItem
                  import com.arcgismaps.mapping.view.AnalysisOverlay
                  import com.arcgismaps.mapping.view.ScreenCoordinate
                  import com.arcgismaps.portal.Portal
                  import com.arcgismaps.toolkit.geoviewcompose.SceneView
                  import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
                  import com.example.app.R
                  import kotlinx.coroutines.launch
                  import kotlin.math.roundToInt
                  class SceneViewModel : ViewModel() {
                  private val portal = Portal(
                  url = "https://www.arcgis.com",
                  connection = Portal.Connection.Anonymous
                  )
                  private val portalItem = PortalItem(
                  portal = portal,
                  itemId = "7558ee942b2547019f66885c44d4f0b1"
                  )
                  var scene = ArcGISScene(portalItem)
                  val sceneViewProxy = SceneViewProxy()
                  // The analysis overlay to be added to the scene.
                  val analysisOverlay = AnalysisOverlay()
                  var viewshed = ExploratoryLocationViewshed(
                  location = Point(x = 0.0, y = 0.0),
                  heading = 0.0,
                  pitch = 90.0,
                  horizontalAngle = 360.0,
                  verticalAngle = 180.0,
                  minDistance = 10.0,
                  maxDistance = 12_000.0
                  )
                  8 collapsed lines
                  }
                  @Composable
                  fun MainScreen() {
                  }
                  • In the init block, make the viewshed not visible upon launch and add the viewshed to the analysis overlay. Then load the scene. Since load() is a suspend function, call it from viewModelScope.launch.

                    SceneViewModel class
                    71 collapsed lines
                    @file:OptIn(ExperimentalMaterial3Api::class)
                    package com.example.app.screens
                    import android.util.Log
                    import androidx.compose.foundation.layout.Arrangement
                    import androidx.compose.foundation.layout.Column
                    import androidx.compose.foundation.layout.Row
                    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.Slider
                    import androidx.compose.material3.Text
                    import androidx.compose.material3.TextButton
                    import androidx.compose.material3.TopAppBar
                    import androidx.compose.runtime.Composable
                    import androidx.compose.runtime.getValue
                    import androidx.compose.runtime.mutableFloatStateOf
                    import androidx.compose.runtime.remember
                    import androidx.compose.runtime.setValue
                    import androidx.compose.ui.Alignment
                    import androidx.compose.ui.Modifier
                    import androidx.compose.ui.res.stringResource
                    import androidx.compose.ui.unit.dp
                    import androidx.lifecycle.ViewModel
                    import androidx.lifecycle.viewModelScope
                    import androidx.lifecycle.viewmodel.compose.viewModel
                    import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
                    import com.arcgismaps.geometry.Point
                    import com.arcgismaps.mapping.ArcGISScene
                    import com.arcgismaps.mapping.PortalItem
                    import com.arcgismaps.mapping.view.AnalysisOverlay
                    import com.arcgismaps.mapping.view.ScreenCoordinate
                    import com.arcgismaps.portal.Portal
                    import com.arcgismaps.toolkit.geoviewcompose.SceneView
                    import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
                    import com.example.app.R
                    import kotlinx.coroutines.launch
                    import kotlin.math.roundToInt
                    class SceneViewModel : ViewModel() {
                    private val portal = Portal(
                    url = "https://www.arcgis.com",
                    connection = Portal.Connection.Anonymous
                    )
                    private val portalItem = PortalItem(
                    portal = portal,
                    itemId = "7558ee942b2547019f66885c44d4f0b1"
                    )
                    var scene = ArcGISScene(portalItem)
                    val sceneViewProxy = SceneViewProxy()
                    // The analysis overlay to be added to the scene.
                    val analysisOverlay = AnalysisOverlay()
                    var viewshed = ExploratoryLocationViewshed(
                    location = Point(x = 0.0, y = 0.0),
                    heading = 0.0,
                    pitch = 90.0,
                    horizontalAngle = 360.0,
                    verticalAngle = 180.0,
                    minDistance = 10.0,
                    maxDistance = 12_000.0
                    )
                    init {
                    viewshed.isVisible = false
                    analysisOverlay.analyses.add(viewshed)
                    viewModelScope.launch {
                    scene.load().getOrElse { logError(it) }
                    }
                    }
                    8 collapsed lines
                    }
                    @Composable
                    fun MainScreen() {
                    }
                    • Define a function named setViewshedLocation() that takes a Point as a parameter. This function sets the location of the viewshed and makes it visible, if it is not visible already.

                      SceneViewModel class
                      80 collapsed lines
                      @file:OptIn(ExperimentalMaterial3Api::class)
                      package com.example.app.screens
                      import android.util.Log
                      import androidx.compose.foundation.layout.Arrangement
                      import androidx.compose.foundation.layout.Column
                      import androidx.compose.foundation.layout.Row
                      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.Slider
                      import androidx.compose.material3.Text
                      import androidx.compose.material3.TextButton
                      import androidx.compose.material3.TopAppBar
                      import androidx.compose.runtime.Composable
                      import androidx.compose.runtime.getValue
                      import androidx.compose.runtime.mutableFloatStateOf
                      import androidx.compose.runtime.remember
                      import androidx.compose.runtime.setValue
                      import androidx.compose.ui.Alignment
                      import androidx.compose.ui.Modifier
                      import androidx.compose.ui.res.stringResource
                      import androidx.compose.ui.unit.dp
                      import androidx.lifecycle.ViewModel
                      import androidx.lifecycle.viewModelScope
                      import androidx.lifecycle.viewmodel.compose.viewModel
                      import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
                      import com.arcgismaps.geometry.Point
                      import com.arcgismaps.mapping.ArcGISScene
                      import com.arcgismaps.mapping.PortalItem
                      import com.arcgismaps.mapping.view.AnalysisOverlay
                      import com.arcgismaps.mapping.view.ScreenCoordinate
                      import com.arcgismaps.portal.Portal
                      import com.arcgismaps.toolkit.geoviewcompose.SceneView
                      import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
                      import com.example.app.R
                      import kotlinx.coroutines.launch
                      import kotlin.math.roundToInt
                      class SceneViewModel : ViewModel() {
                      private val portal = Portal(
                      url = "https://www.arcgis.com",
                      connection = Portal.Connection.Anonymous
                      )
                      private val portalItem = PortalItem(
                      portal = portal,
                      itemId = "7558ee942b2547019f66885c44d4f0b1"
                      )
                      var scene = ArcGISScene(portalItem)
                      val sceneViewProxy = SceneViewProxy()
                      // The analysis overlay to be added to the scene.
                      val analysisOverlay = AnalysisOverlay()
                      var viewshed = ExploratoryLocationViewshed(
                      location = Point(x = 0.0, y = 0.0),
                      heading = 0.0,
                      pitch = 90.0,
                      horizontalAngle = 360.0,
                      verticalAngle = 180.0,
                      minDistance = 10.0,
                      maxDistance = 12_000.0
                      )
                      init {
                      viewshed.isVisible = false
                      analysisOverlay.analyses.add(viewshed)
                      viewModelScope.launch {
                      scene.load().getOrElse { logError(it) }
                      }
                      }
                      private fun setViewshedLocation(point: Point) {
                      viewshed.location = point
                      viewshed.isVisible = true
                      }
                      8 collapsed lines
                      }
                      @Composable
                      fun MainScreen() {
                      }
                      • Define a function named hideViewshed(), which hides the viewshed without modifying or deleting it. Then create a function named setViewshedDistance() to set a new maximum distance value for the viewshed.

                        SceneViewModel class
                        85 collapsed lines
                        @file:OptIn(ExperimentalMaterial3Api::class)
                        package com.example.app.screens
                        import android.util.Log
                        import androidx.compose.foundation.layout.Arrangement
                        import androidx.compose.foundation.layout.Column
                        import androidx.compose.foundation.layout.Row
                        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.Slider
                        import androidx.compose.material3.Text
                        import androidx.compose.material3.TextButton
                        import androidx.compose.material3.TopAppBar
                        import androidx.compose.runtime.Composable
                        import androidx.compose.runtime.getValue
                        import androidx.compose.runtime.mutableFloatStateOf
                        import androidx.compose.runtime.remember
                        import androidx.compose.runtime.setValue
                        import androidx.compose.ui.Alignment
                        import androidx.compose.ui.Modifier
                        import androidx.compose.ui.res.stringResource
                        import androidx.compose.ui.unit.dp
                        import androidx.lifecycle.ViewModel
                        import androidx.lifecycle.viewModelScope
                        import androidx.lifecycle.viewmodel.compose.viewModel
                        import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
                        import com.arcgismaps.geometry.Point
                        import com.arcgismaps.mapping.ArcGISScene
                        import com.arcgismaps.mapping.PortalItem
                        import com.arcgismaps.mapping.view.AnalysisOverlay
                        import com.arcgismaps.mapping.view.ScreenCoordinate
                        import com.arcgismaps.portal.Portal
                        import com.arcgismaps.toolkit.geoviewcompose.SceneView
                        import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
                        import com.example.app.R
                        import kotlinx.coroutines.launch
                        import kotlin.math.roundToInt
                        class SceneViewModel : ViewModel() {
                        private val portal = Portal(
                        url = "https://www.arcgis.com",
                        connection = Portal.Connection.Anonymous
                        )
                        private val portalItem = PortalItem(
                        portal = portal,
                        itemId = "7558ee942b2547019f66885c44d4f0b1"
                        )
                        var scene = ArcGISScene(portalItem)
                        val sceneViewProxy = SceneViewProxy()
                        // The analysis overlay to be added to the scene.
                        val analysisOverlay = AnalysisOverlay()
                        var viewshed = ExploratoryLocationViewshed(
                        location = Point(x = 0.0, y = 0.0),
                        heading = 0.0,
                        pitch = 90.0,
                        horizontalAngle = 360.0,
                        verticalAngle = 180.0,
                        minDistance = 10.0,
                        maxDistance = 12_000.0
                        )
                        init {
                        viewshed.isVisible = false
                        analysisOverlay.analyses.add(viewshed)
                        viewModelScope.launch {
                        scene.load().getOrElse { logError(it) }
                        }
                        }
                        private fun setViewshedLocation(point: Point) {
                        viewshed.location = point
                        viewshed.isVisible = true
                        }
                        fun hideViewshed() {
                        viewshed.isVisible = false
                        }
                        fun setViewshedDistance(maxDistance: Double) {
                        viewshed.maxDistance = maxDistance
                        }
                        8 collapsed lines
                        }
                        @Composable
                        fun MainScreen() {
                        }

                        Display the viewshed analysis with touch events

                        Touch events determine where to place the observer for the viewshed analysis. A user will long-press and drag to reveal and move the observer’s location.

                        1. Define a function named handleLongPressEvent() to add or move the viewshed analysis when the user long-presses on the scene. To obtain the new location (Point) in the scene, call screenToLocation() on the SceneViewProxy, passing in the screen coordinate of the long-press. Then call your setViewshedLocation() with the scene point.

                          SceneViewModel class
                          93 collapsed lines
                          @file:OptIn(ExperimentalMaterial3Api::class)
                          package com.example.app.screens
                          import android.util.Log
                          import androidx.compose.foundation.layout.Arrangement
                          import androidx.compose.foundation.layout.Column
                          import androidx.compose.foundation.layout.Row
                          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.Slider
                          import androidx.compose.material3.Text
                          import androidx.compose.material3.TextButton
                          import androidx.compose.material3.TopAppBar
                          import androidx.compose.runtime.Composable
                          import androidx.compose.runtime.getValue
                          import androidx.compose.runtime.mutableFloatStateOf
                          import androidx.compose.runtime.remember
                          import androidx.compose.runtime.setValue
                          import androidx.compose.ui.Alignment
                          import androidx.compose.ui.Modifier
                          import androidx.compose.ui.res.stringResource
                          import androidx.compose.ui.unit.dp
                          import androidx.lifecycle.ViewModel
                          import androidx.lifecycle.viewModelScope
                          import androidx.lifecycle.viewmodel.compose.viewModel
                          import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
                          import com.arcgismaps.geometry.Point
                          import com.arcgismaps.mapping.ArcGISScene
                          import com.arcgismaps.mapping.PortalItem
                          import com.arcgismaps.mapping.view.AnalysisOverlay
                          import com.arcgismaps.mapping.view.ScreenCoordinate
                          import com.arcgismaps.portal.Portal
                          import com.arcgismaps.toolkit.geoviewcompose.SceneView
                          import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
                          import com.example.app.R
                          import kotlinx.coroutines.launch
                          import kotlin.math.roundToInt
                          class SceneViewModel : ViewModel() {
                          private val portal = Portal(
                          url = "https://www.arcgis.com",
                          connection = Portal.Connection.Anonymous
                          )
                          private val portalItem = PortalItem(
                          portal = portal,
                          itemId = "7558ee942b2547019f66885c44d4f0b1"
                          )
                          var scene = ArcGISScene(portalItem)
                          val sceneViewProxy = SceneViewProxy()
                          // The analysis overlay to be added to the scene.
                          val analysisOverlay = AnalysisOverlay()
                          var viewshed = ExploratoryLocationViewshed(
                          location = Point(x = 0.0, y = 0.0),
                          heading = 0.0,
                          pitch = 90.0,
                          horizontalAngle = 360.0,
                          verticalAngle = 180.0,
                          minDistance = 10.0,
                          maxDistance = 12_000.0
                          )
                          init {
                          viewshed.isVisible = false
                          analysisOverlay.analyses.add(viewshed)
                          viewModelScope.launch {
                          scene.load().getOrElse { logError(it) }
                          }
                          }
                          private fun setViewshedLocation(point: Point) {
                          viewshed.location = point
                          viewshed.isVisible = true
                          }
                          fun hideViewshed() {
                          viewshed.isVisible = false
                          }
                          fun setViewshedDistance(maxDistance: Double) {
                          viewshed.maxDistance = maxDistance
                          }
                          fun handleOnLongPress(screenCoordinate: ScreenCoordinate) {
                          viewModelScope.launch {
                          val scenePoint = sceneViewProxy.screenToLocation(screenCoordinate).getOrElse { error ->
                          return@launch logError(error)
                          }
                          setViewshedLocation(scenePoint)
                          }
                          }
                          8 collapsed lines
                          }
                          @Composable
                          fun MainScreen() {
                          }
                          • Define a function to log errors.

                            SceneViewModel class
                            102 collapsed lines
                            @file:OptIn(ExperimentalMaterial3Api::class)
                            package com.example.app.screens
                            import android.util.Log
                            import androidx.compose.foundation.layout.Arrangement
                            import androidx.compose.foundation.layout.Column
                            import androidx.compose.foundation.layout.Row
                            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.Slider
                            import androidx.compose.material3.Text
                            import androidx.compose.material3.TextButton
                            import androidx.compose.material3.TopAppBar
                            import androidx.compose.runtime.Composable
                            import androidx.compose.runtime.getValue
                            import androidx.compose.runtime.mutableFloatStateOf
                            import androidx.compose.runtime.remember
                            import androidx.compose.runtime.setValue
                            import androidx.compose.ui.Alignment
                            import androidx.compose.ui.Modifier
                            import androidx.compose.ui.res.stringResource
                            import androidx.compose.ui.unit.dp
                            import androidx.lifecycle.ViewModel
                            import androidx.lifecycle.viewModelScope
                            import androidx.lifecycle.viewmodel.compose.viewModel
                            import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
                            import com.arcgismaps.geometry.Point
                            import com.arcgismaps.mapping.ArcGISScene
                            import com.arcgismaps.mapping.PortalItem
                            import com.arcgismaps.mapping.view.AnalysisOverlay
                            import com.arcgismaps.mapping.view.ScreenCoordinate
                            import com.arcgismaps.portal.Portal
                            import com.arcgismaps.toolkit.geoviewcompose.SceneView
                            import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
                            import com.example.app.R
                            import kotlinx.coroutines.launch
                            import kotlin.math.roundToInt
                            class SceneViewModel : ViewModel() {
                            private val portal = Portal(
                            url = "https://www.arcgis.com",
                            connection = Portal.Connection.Anonymous
                            )
                            private val portalItem = PortalItem(
                            portal = portal,
                            itemId = "7558ee942b2547019f66885c44d4f0b1"
                            )
                            var scene = ArcGISScene(portalItem)
                            val sceneViewProxy = SceneViewProxy()
                            // The analysis overlay to be added to the scene.
                            val analysisOverlay = AnalysisOverlay()
                            var viewshed = ExploratoryLocationViewshed(
                            location = Point(x = 0.0, y = 0.0),
                            heading = 0.0,
                            pitch = 90.0,
                            horizontalAngle = 360.0,
                            verticalAngle = 180.0,
                            minDistance = 10.0,
                            maxDistance = 12_000.0
                            )
                            init {
                            viewshed.isVisible = false
                            analysisOverlay.analyses.add(viewshed)
                            viewModelScope.launch {
                            scene.load().getOrElse { logError(it) }
                            }
                            }
                            private fun setViewshedLocation(point: Point) {
                            viewshed.location = point
                            viewshed.isVisible = true
                            }
                            fun hideViewshed() {
                            viewshed.isVisible = false
                            }
                            fun setViewshedDistance(maxDistance: Double) {
                            viewshed.maxDistance = maxDistance
                            }
                            fun handleOnLongPress(screenCoordinate: ScreenCoordinate) {
                            viewModelScope.launch {
                            val scenePoint = sceneViewProxy.screenToLocation(screenCoordinate).getOrElse { error ->
                            return@launch logError(error)
                            }
                            setViewshedLocation(scenePoint)
                            }
                            }
                            private fun logError(error: Throwable) {
                            Log.e(this.javaClass.simpleName, error.message.toString(), error.cause)
                            }
                            8 collapsed lines
                            }
                            @Composable
                            fun MainScreen() {
                            }

                            Add a UI to control the viewshed analysis

                            To control the viewshed analysis, some UI elements are required.

                            1. Create variables in the MainScreen composable.

                              • Create sceneViewModel using viewModel().
                              • Create sliderValue of type MutableFloatState. Since viewshed.maxDistance in the view model is a Double, but the value passed to Jetpack Compose Slider composable is a Float, you must convert the max distance to a Float.
                              MainScreen()
                              109 collapsed lines
                              @file:OptIn(ExperimentalMaterial3Api::class)
                              package com.example.app.screens
                              import android.util.Log
                              import androidx.compose.foundation.layout.Arrangement
                              import androidx.compose.foundation.layout.Column
                              import androidx.compose.foundation.layout.Row
                              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.Slider
                              import androidx.compose.material3.Text
                              import androidx.compose.material3.TextButton
                              import androidx.compose.material3.TopAppBar
                              import androidx.compose.runtime.Composable
                              import androidx.compose.runtime.getValue
                              import androidx.compose.runtime.mutableFloatStateOf
                              import androidx.compose.runtime.remember
                              import androidx.compose.runtime.setValue
                              import androidx.compose.ui.Alignment
                              import androidx.compose.ui.Modifier
                              import androidx.compose.ui.res.stringResource
                              import androidx.compose.ui.unit.dp
                              import androidx.lifecycle.ViewModel
                              import androidx.lifecycle.viewModelScope
                              import androidx.lifecycle.viewmodel.compose.viewModel
                              import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
                              import com.arcgismaps.geometry.Point
                              import com.arcgismaps.mapping.ArcGISScene
                              import com.arcgismaps.mapping.PortalItem
                              import com.arcgismaps.mapping.view.AnalysisOverlay
                              import com.arcgismaps.mapping.view.ScreenCoordinate
                              import com.arcgismaps.portal.Portal
                              import com.arcgismaps.toolkit.geoviewcompose.SceneView
                              import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
                              import com.example.app.R
                              import kotlinx.coroutines.launch
                              import kotlin.math.roundToInt
                              class SceneViewModel : ViewModel() {
                              private val portal = Portal(
                              url = "https://www.arcgis.com",
                              connection = Portal.Connection.Anonymous
                              )
                              private val portalItem = PortalItem(
                              portal = portal,
                              itemId = "7558ee942b2547019f66885c44d4f0b1"
                              )
                              var scene = ArcGISScene(portalItem)
                              val sceneViewProxy = SceneViewProxy()
                              // The analysis overlay to be added to the scene.
                              val analysisOverlay = AnalysisOverlay()
                              var viewshed = ExploratoryLocationViewshed(
                              location = Point(x = 0.0, y = 0.0),
                              heading = 0.0,
                              pitch = 90.0,
                              horizontalAngle = 360.0,
                              verticalAngle = 180.0,
                              minDistance = 10.0,
                              maxDistance = 12_000.0
                              )
                              init {
                              viewshed.isVisible = false
                              analysisOverlay.analyses.add(viewshed)
                              viewModelScope.launch {
                              scene.load().getOrElse { logError(it) }
                              }
                              }
                              private fun setViewshedLocation(point: Point) {
                              viewshed.location = point
                              viewshed.isVisible = true
                              }
                              fun hideViewshed() {
                              viewshed.isVisible = false
                              }
                              fun setViewshedDistance(maxDistance: Double) {
                              viewshed.maxDistance = maxDistance
                              }
                              fun handleOnLongPress(screenCoordinate: ScreenCoordinate) {
                              viewModelScope.launch {
                              val scenePoint = sceneViewProxy.screenToLocation(screenCoordinate).getOrElse { error ->
                              return@launch logError(error)
                              }
                              setViewshedLocation(scenePoint)
                              }
                              }
                              private fun logError(error: Throwable) {
                              Log.e(this.javaClass.simpleName, error.message.toString(), error.cause)
                              }
                              }
                              @Composable
                              fun MainScreen() {
                              val sceneViewModel: SceneViewModel = viewModel()
                              // If viewshed.maxDistance is not null, assign it to maxDistance. Otherwise, assign default of 1000.0
                              var sliderValue by remember {
                              mutableFloatStateOf(sceneViewModel.viewshed.maxDistance?.toFloat() ?: 1000.0f)
                              }
                              }
                              • Add a Scaffold and pass a TopAppBar. The Scaffold’s content lambda contains a Column.

                                MainScreen()
                                109 collapsed lines
                                @file:OptIn(ExperimentalMaterial3Api::class)
                                package com.example.app.screens
                                import android.util.Log
                                import androidx.compose.foundation.layout.Arrangement
                                import androidx.compose.foundation.layout.Column
                                import androidx.compose.foundation.layout.Row
                                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.Slider
                                import androidx.compose.material3.Text
                                import androidx.compose.material3.TextButton
                                import androidx.compose.material3.TopAppBar
                                import androidx.compose.runtime.Composable
                                import androidx.compose.runtime.getValue
                                import androidx.compose.runtime.mutableFloatStateOf
                                import androidx.compose.runtime.remember
                                import androidx.compose.runtime.setValue
                                import androidx.compose.ui.Alignment
                                import androidx.compose.ui.Modifier
                                import androidx.compose.ui.res.stringResource
                                import androidx.compose.ui.unit.dp
                                import androidx.lifecycle.ViewModel
                                import androidx.lifecycle.viewModelScope
                                import androidx.lifecycle.viewmodel.compose.viewModel
                                import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
                                import com.arcgismaps.geometry.Point
                                import com.arcgismaps.mapping.ArcGISScene
                                import com.arcgismaps.mapping.PortalItem
                                import com.arcgismaps.mapping.view.AnalysisOverlay
                                import com.arcgismaps.mapping.view.ScreenCoordinate
                                import com.arcgismaps.portal.Portal
                                import com.arcgismaps.toolkit.geoviewcompose.SceneView
                                import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
                                import com.example.app.R
                                import kotlinx.coroutines.launch
                                import kotlin.math.roundToInt
                                class SceneViewModel : ViewModel() {
                                private val portal = Portal(
                                url = "https://www.arcgis.com",
                                connection = Portal.Connection.Anonymous
                                )
                                private val portalItem = PortalItem(
                                portal = portal,
                                itemId = "7558ee942b2547019f66885c44d4f0b1"
                                )
                                var scene = ArcGISScene(portalItem)
                                val sceneViewProxy = SceneViewProxy()
                                // The analysis overlay to be added to the scene.
                                val analysisOverlay = AnalysisOverlay()
                                var viewshed = ExploratoryLocationViewshed(
                                location = Point(x = 0.0, y = 0.0),
                                heading = 0.0,
                                pitch = 90.0,
                                horizontalAngle = 360.0,
                                verticalAngle = 180.0,
                                minDistance = 10.0,
                                maxDistance = 12_000.0
                                )
                                init {
                                viewshed.isVisible = false
                                analysisOverlay.analyses.add(viewshed)
                                viewModelScope.launch {
                                scene.load().getOrElse { logError(it) }
                                }
                                }
                                private fun setViewshedLocation(point: Point) {
                                viewshed.location = point
                                viewshed.isVisible = true
                                }
                                fun hideViewshed() {
                                viewshed.isVisible = false
                                }
                                fun setViewshedDistance(maxDistance: Double) {
                                viewshed.maxDistance = maxDistance
                                }
                                fun handleOnLongPress(screenCoordinate: ScreenCoordinate) {
                                viewModelScope.launch {
                                val scenePoint = sceneViewProxy.screenToLocation(screenCoordinate).getOrElse { error ->
                                return@launch logError(error)
                                }
                                setViewshedLocation(scenePoint)
                                }
                                }
                                private fun logError(error: Throwable) {
                                Log.e(this.javaClass.simpleName, error.message.toString(), error.cause)
                                }
                                }
                                @Composable
                                fun MainScreen() {
                                val sceneViewModel: SceneViewModel = viewModel()
                                // If viewshed.maxDistance is not null, assign it to maxDistance. Otherwise, assign default of 1000.0
                                var sliderValue by remember {
                                mutableFloatStateOf(sceneViewModel.viewshed.maxDistance?.toFloat() ?: 1000.0f)
                                }
                                Scaffold(
                                topBar = { TopAppBar(title = { Text(text = stringResource(id = R.string.app_name)) }) }
                                ) { innerPadding ->
                                Column(
                                modifier = Modifier.padding(innerPadding),
                                horizontalAlignment = Alignment.CenterHorizontally,
                                verticalArrangement = Arrangement.spacedBy(12.dp)
                                ) {
                                }
                                }
                                }
                                • In the Column’s content lambda, add the SceneView composable. For the arcGISScene, sceneViewProxy, and analysisOverlays parameters, pass the corresponding properties from sceneViewModel. For onLongPress, pass a lambda that calls sceneViewModel.handleOnLongPress().

                                  MainScreen()
                                  118 collapsed lines
                                  @file:OptIn(ExperimentalMaterial3Api::class)
                                  package com.example.app.screens
                                  import android.util.Log
                                  import androidx.compose.foundation.layout.Arrangement
                                  import androidx.compose.foundation.layout.Column
                                  import androidx.compose.foundation.layout.Row
                                  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.Slider
                                  import androidx.compose.material3.Text
                                  import androidx.compose.material3.TextButton
                                  import androidx.compose.material3.TopAppBar
                                  import androidx.compose.runtime.Composable
                                  import androidx.compose.runtime.getValue
                                  import androidx.compose.runtime.mutableFloatStateOf
                                  import androidx.compose.runtime.remember
                                  import androidx.compose.runtime.setValue
                                  import androidx.compose.ui.Alignment
                                  import androidx.compose.ui.Modifier
                                  import androidx.compose.ui.res.stringResource
                                  import androidx.compose.ui.unit.dp
                                  import androidx.lifecycle.ViewModel
                                  import androidx.lifecycle.viewModelScope
                                  import androidx.lifecycle.viewmodel.compose.viewModel
                                  import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
                                  import com.arcgismaps.geometry.Point
                                  import com.arcgismaps.mapping.ArcGISScene
                                  import com.arcgismaps.mapping.PortalItem
                                  import com.arcgismaps.mapping.view.AnalysisOverlay
                                  import com.arcgismaps.mapping.view.ScreenCoordinate
                                  import com.arcgismaps.portal.Portal
                                  import com.arcgismaps.toolkit.geoviewcompose.SceneView
                                  import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
                                  import com.example.app.R
                                  import kotlinx.coroutines.launch
                                  import kotlin.math.roundToInt
                                  class SceneViewModel : ViewModel() {
                                  private val portal = Portal(
                                  url = "https://www.arcgis.com",
                                  connection = Portal.Connection.Anonymous
                                  )
                                  private val portalItem = PortalItem(
                                  portal = portal,
                                  itemId = "7558ee942b2547019f66885c44d4f0b1"
                                  )
                                  var scene = ArcGISScene(portalItem)
                                  val sceneViewProxy = SceneViewProxy()
                                  // The analysis overlay to be added to the scene.
                                  val analysisOverlay = AnalysisOverlay()
                                  var viewshed = ExploratoryLocationViewshed(
                                  location = Point(x = 0.0, y = 0.0),
                                  heading = 0.0,
                                  pitch = 90.0,
                                  horizontalAngle = 360.0,
                                  verticalAngle = 180.0,
                                  minDistance = 10.0,
                                  maxDistance = 12_000.0
                                  )
                                  init {
                                  viewshed.isVisible = false
                                  analysisOverlay.analyses.add(viewshed)
                                  viewModelScope.launch {
                                  scene.load().getOrElse { logError(it) }
                                  }
                                  }
                                  private fun setViewshedLocation(point: Point) {
                                  viewshed.location = point
                                  viewshed.isVisible = true
                                  }
                                  fun hideViewshed() {
                                  viewshed.isVisible = false
                                  }
                                  fun setViewshedDistance(maxDistance: Double) {
                                  viewshed.maxDistance = maxDistance
                                  }
                                  fun handleOnLongPress(screenCoordinate: ScreenCoordinate) {
                                  viewModelScope.launch {
                                  val scenePoint = sceneViewProxy.screenToLocation(screenCoordinate).getOrElse { error ->
                                  return@launch logError(error)
                                  }
                                  setViewshedLocation(scenePoint)
                                  }
                                  }
                                  private fun logError(error: Throwable) {
                                  Log.e(this.javaClass.simpleName, error.message.toString(), error.cause)
                                  }
                                  }
                                  @Composable
                                  fun MainScreen() {
                                  val sceneViewModel: SceneViewModel = viewModel()
                                  // If viewshed.maxDistance is not null, assign it to maxDistance. Otherwise, assign default of 1000.0
                                  var sliderValue by remember {
                                  mutableFloatStateOf(sceneViewModel.viewshed.maxDistance?.toFloat() ?: 1000.0f)
                                  }
                                  Scaffold(
                                  topBar = { TopAppBar(title = { Text(text = stringResource(id = R.string.app_name)) }) }
                                  ) { innerPadding ->
                                  Column(
                                  modifier = Modifier.padding(innerPadding),
                                  horizontalAlignment = Alignment.CenterHorizontally,
                                  verticalArrangement = Arrangement.spacedBy(12.dp)
                                  ) {
                                  SceneView(
                                  modifier = Modifier
                                  .fillMaxSize()
                                  .weight(1f),
                                  arcGISScene = sceneViewModel.scene,
                                  sceneViewProxy = sceneViewModel.sceneViewProxy,
                                  analysisOverlays = listOf(sceneViewModel.analysisOverlay),
                                  onLongPress = { longPressEvent ->
                                  sceneViewModel.handleOnLongPress(longPressEvent.screenCoordinate)
                                  }
                                  )
                                  }
                                  }
                                  2 collapsed lines
                                  }
                                  • Add a Text to display the current value of the slider.

                                  • Add a Row that will contain a Slider and a TextButton.

                                    The slider allows the user to adjust the maximum distance of the viewshed in the range of 0f..20999f. The value parameter is the current value of the slider, which is held in the sliderValue variable of the MainScreen composable. For the onValuechange parameters pass a lambda that saves the new value of the slider in the sliderValue variable and then calls setViewshedDistance() in the view model. Changing the maximum distance of the viewshed expands or contracts the size of the observer’s field of view.

                                    The text button says “Clear” and calls the view model’s hideViewshed() when clicked. This allows the user a fresh start to make more analyses.

                                    MainScreen()
                                    139 collapsed lines
                                    @file:OptIn(ExperimentalMaterial3Api::class)
                                    package com.example.app.screens
                                    import android.util.Log
                                    import androidx.compose.foundation.layout.Arrangement
                                    import androidx.compose.foundation.layout.Column
                                    import androidx.compose.foundation.layout.Row
                                    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.Slider
                                    import androidx.compose.material3.Text
                                    import androidx.compose.material3.TextButton
                                    import androidx.compose.material3.TopAppBar
                                    import androidx.compose.runtime.Composable
                                    import androidx.compose.runtime.getValue
                                    import androidx.compose.runtime.mutableFloatStateOf
                                    import androidx.compose.runtime.remember
                                    import androidx.compose.runtime.setValue
                                    import androidx.compose.ui.Alignment
                                    import androidx.compose.ui.Modifier
                                    import androidx.compose.ui.res.stringResource
                                    import androidx.compose.ui.unit.dp
                                    import androidx.lifecycle.ViewModel
                                    import androidx.lifecycle.viewModelScope
                                    import androidx.lifecycle.viewmodel.compose.viewModel
                                    import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
                                    import com.arcgismaps.geometry.Point
                                    import com.arcgismaps.mapping.ArcGISScene
                                    import com.arcgismaps.mapping.PortalItem
                                    import com.arcgismaps.mapping.view.AnalysisOverlay
                                    import com.arcgismaps.mapping.view.ScreenCoordinate
                                    import com.arcgismaps.portal.Portal
                                    import com.arcgismaps.toolkit.geoviewcompose.SceneView
                                    import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
                                    import com.example.app.R
                                    import kotlinx.coroutines.launch
                                    import kotlin.math.roundToInt
                                    class SceneViewModel : ViewModel() {
                                    private val portal = Portal(
                                    url = "https://www.arcgis.com",
                                    connection = Portal.Connection.Anonymous
                                    )
                                    private val portalItem = PortalItem(
                                    portal = portal,
                                    itemId = "7558ee942b2547019f66885c44d4f0b1"
                                    )
                                    var scene = ArcGISScene(portalItem)
                                    val sceneViewProxy = SceneViewProxy()
                                    // The analysis overlay to be added to the scene.
                                    val analysisOverlay = AnalysisOverlay()
                                    var viewshed = ExploratoryLocationViewshed(
                                    location = Point(x = 0.0, y = 0.0),
                                    heading = 0.0,
                                    pitch = 90.0,
                                    horizontalAngle = 360.0,
                                    verticalAngle = 180.0,
                                    minDistance = 10.0,
                                    maxDistance = 12_000.0
                                    )
                                    init {
                                    viewshed.isVisible = false
                                    analysisOverlay.analyses.add(viewshed)
                                    viewModelScope.launch {
                                    scene.load().getOrElse { logError(it) }
                                    }
                                    }
                                    private fun setViewshedLocation(point: Point) {
                                    viewshed.location = point
                                    viewshed.isVisible = true
                                    }
                                    fun hideViewshed() {
                                    viewshed.isVisible = false
                                    }
                                    fun setViewshedDistance(maxDistance: Double) {
                                    viewshed.maxDistance = maxDistance
                                    }
                                    fun handleOnLongPress(screenCoordinate: ScreenCoordinate) {
                                    viewModelScope.launch {
                                    val scenePoint = sceneViewProxy.screenToLocation(screenCoordinate).getOrElse { error ->
                                    return@launch logError(error)
                                    }
                                    setViewshedLocation(scenePoint)
                                    }
                                    }
                                    private fun logError(error: Throwable) {
                                    Log.e(this.javaClass.simpleName, error.message.toString(), error.cause)
                                    }
                                    }
                                    @Composable
                                    fun MainScreen() {
                                    val sceneViewModel: SceneViewModel = viewModel()
                                    // If viewshed.maxDistance is not null, assign it to maxDistance. Otherwise, assign default of 1000.0
                                    var sliderValue by remember {
                                    mutableFloatStateOf(sceneViewModel.viewshed.maxDistance?.toFloat() ?: 1000.0f)
                                    }
                                    Scaffold(
                                    topBar = { TopAppBar(title = { Text(text = stringResource(id = R.string.app_name)) }) }
                                    ) { innerPadding ->
                                    Column(
                                    modifier = Modifier.padding(innerPadding),
                                    horizontalAlignment = Alignment.CenterHorizontally,
                                    verticalArrangement = Arrangement.spacedBy(12.dp)
                                    ) {
                                    SceneView(
                                    modifier = Modifier
                                    .fillMaxSize()
                                    .weight(1f),
                                    arcGISScene = sceneViewModel.scene,
                                    sceneViewProxy = sceneViewModel.sceneViewProxy,
                                    analysisOverlays = listOf(sceneViewModel.analysisOverlay),
                                    onLongPress = { longPressEvent ->
                                    sceneViewModel.handleOnLongPress(longPressEvent.screenCoordinate)
                                    }
                                    )
                                    Text(text = "Viewshed max distance: "+sliderValue.roundToInt().toString())
                                    Row {
                                    Slider(
                                    modifier = Modifier.weight(1f),
                                    value = sliderValue,
                                    valueRange = 0f..20999f,
                                    onValueChange = {
                                    sliderValue = it
                                    sceneViewModel.setViewshedDistance(sliderValue.toDouble())
                                    },
                                    )
                                    TextButton(onClick = { sceneViewModel.hideViewshed() }) {
                                    Text(text = "Clear")
                                    }
                                    }
                                    5 collapsed lines
                                    }
                                    }
                                    }

                                    Click Run > Run > app to run the app.

                                    You should see a scene of hotspots in the Yosemite Valley. Long-press and drag to display and move a viewshed analysis to explore the visibility of terrain from various locations.

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

                                    Option 2: Download the solution

                                    1. Click the Download solution link in the right-hand side of this page.

                                    2. Unzip the file to a location on your machine.

                                    3. Run Android Studio.

                                    4. Go to File > Open…. Navigate to the solution folder and click Open.

                                      On Windows: If you are in the Welcome to Android Studio dialog, click Open and navigate to the solution folder. Then click Open.

                                    Since the downloaded solution does not contain authentication credentials, you must first set up authentication to create credentials, and then add the developer credentials to the solution.

                                    Set up authentication

                                    To access the secure ArcGIS location servicesArcGIS 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 Static Maps 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 authenticationAPI 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 authenticationUser 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 PlatformAn 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 OnlineAn 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 tokenAn 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 privilegesPrivileges 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.

                                    Set developer credentials in the solution

                                    To allow your app users to access ArcGIS location servicesArcGIS 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 Static Maps 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.

                                    1. In the Android view of Android Studio, open app > kotlin+java > com.example.app > MainActivity. Set the AuthenticationMode to .API_KEY.

                                      MainActivity.kt
                                      14 collapsed lines
                                      package com.example.app
                                      import android.os.Bundle
                                      import androidx.activity.ComponentActivity
                                      import androidx.activity.compose.setContent
                                      import androidx.activity.enableEdgeToEdge
                                      import com.arcgismaps.ApiKey
                                      import com.arcgismaps.ArcGISEnvironment
                                      import com.arcgismaps.httpcore.authentication.OAuthUserConfiguration
                                      import com.arcgismaps.toolkit.authentication.AuthenticatorState
                                      import com.arcgismaps.toolkit.authentication.DialogAuthenticator
                                      import com.example.app.screens.MainScreen
                                      import com.example.app.ui.theme.TutorialTheme
                                      class MainActivity : ComponentActivity() {
                                      private enum class AuthenticationMode { API_KEY, USER_AUTH }
                                      private val authenticationMode = AuthenticationMode.API_KEY
                                      42 collapsed lines
                                      private val authenticatorState = AuthenticatorState()
                                      override fun onCreate(savedInstanceState: Bundle?) {
                                      super.onCreate(savedInstanceState)
                                      when (authenticationMode) {
                                      AuthenticationMode.API_KEY -> {
                                      ArcGISEnvironment.apiKey = ApiKey.create("YOUR_ACCESS_TOKEN")
                                      }
                                      AuthenticationMode.USER_AUTH -> {
                                      authenticatorState.oAuthUserConfigurations = listOf(
                                      OAuthUserConfiguration(
                                      portalUrl = "https://www.arcgis.com",
                                      clientId = "YOUR_CLIENT_ID",
                                      redirectUrl = "YOUR_REDIRECT_URL"
                                      )
                                      )
                                      }
                                      }
                                      enableEdgeToEdge()
                                      setContent {
                                      TutorialTheme {
                                      MainScreen()
                                      if (authenticationMode == AuthenticationMode.USER_AUTH) {
                                      DialogAuthenticator(authenticatorState)
                                      }
                                      }
                                      }
                                      }
                                      }
                                      • Set the apiKey property with your API key access token.

                                        MainActivity.kt
                                        22 collapsed lines
                                        package com.example.app
                                        import android.os.Bundle
                                        import androidx.activity.ComponentActivity
                                        import androidx.activity.compose.setContent
                                        import androidx.activity.enableEdgeToEdge
                                        import com.arcgismaps.ApiKey
                                        import com.arcgismaps.ArcGISEnvironment
                                        import com.arcgismaps.httpcore.authentication.OAuthUserConfiguration
                                        import com.arcgismaps.toolkit.authentication.AuthenticatorState
                                        import com.arcgismaps.toolkit.authentication.DialogAuthenticator
                                        import com.example.app.screens.MainScreen
                                        import com.example.app.ui.theme.TutorialTheme
                                        class MainActivity : ComponentActivity() {
                                        private enum class AuthenticationMode { API_KEY, USER_AUTH }
                                        private val authenticationMode = AuthenticationMode.API_KEY
                                        private val authenticatorState = AuthenticatorState()
                                        override fun onCreate(savedInstanceState: Bundle?) {
                                        super.onCreate(savedInstanceState)
                                        when (authenticationMode) {
                                        AuthenticationMode.API_KEY -> {
                                        ArcGISEnvironment.apiKey = ApiKey.create("YOUR_ACCESS_TOKEN")
                                        }
                                        30 collapsed lines
                                        AuthenticationMode.USER_AUTH -> {
                                        authenticatorState.oAuthUserConfigurations = listOf(
                                        OAuthUserConfiguration(
                                        portalUrl = "https://www.arcgis.com",
                                        clientId = "YOUR_CLIENT_ID",
                                        redirectUrl = "YOUR_REDIRECT_URL"
                                        )
                                        )
                                        }
                                        }
                                        enableEdgeToEdge()
                                        setContent {
                                        TutorialTheme {
                                        MainScreen()
                                        if (authenticationMode == AuthenticationMode.USER_AUTH) {
                                        DialogAuthenticator(authenticatorState)
                                        }
                                        }
                                        }
                                        }
                                        }

                                        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 app

                                        Click Run > Run > app to run the app.

                                        You should see a scene of trails, trailheads and parks in the Santa Monica Mountains. Drag, swipe, or pinch 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: