Play tours in KML files.

Use case
KML, the file format used by Google Earth, supports creating tours, which can control the viewpoint of the scene, hide and show content, and play audio. Tours allow you to easily share tours of geographic locations, which can be augmented with rich multimedia. Runtime allows you to consume these tours using a simple API.
How to use the sample
The sample will load the KMZ file from ArcGIS Online. When a tour is found, the Play button will be enabled. Use Play and Pause to control the tour. When you’re ready to show the tour, use the reset button to return the tour to the unplayed state.
How it works
- Create a
KmlDataSetfrom the local kmz file and instantiate a layer from it withKmlLayer(kmlDataSet). - Create the KML tour controller. Wire up the buttons to the
KmlTourController.play(),KmlTourController.pause(), andKmlTourController.reset()methods. - Explore the tree of KML content to find the first KML tour. Once a tour is found, provide it to the KML tour controller.
Relevant API
- KmlTour
- KmlTourController
- KmlTourController.pause()
- KmlTourController.play()
- KmlTourController.reset()
About the data
This sample uses a custom tour from ArcGIS Online. When you play the tour, you’ll go through a audio journey through some of Esri’s offices.
Additional information
See Touring in KML in Keyhole Markup Language for more information.
This sample uses the GeoView-Compose Toolkit module to be able to implement a composable SceneView.
Tags
animation, geoview-compose, interactive, KML, narration, pause, play, story, toolkit, tour
Sample Code
/* Copyright 2024 Esri * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */
package com.esri.arcgismaps.sample.playkmltour
import android.content.Intentimport android.os.Bundleimport com.esri.arcgismaps.sample.sampleslib.DownloaderActivity
class DownloadActivity : DownloaderActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) downloadAndStartSample( Intent(this, MainActivity::class.java), // get the app name of the sample getString(R.string.play_kml_tour_app_name), listOf( // ArcGIS Portal item containing the .kmz markup tour file // of Esri HQ and some of the global offices "https://arcgisruntime.maps.arcgis.com/home/item.html?id=f10b1d37fdd645c9bc9b189fb546307c" ) ) }}/* Copyright 2024 Esri * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */
package com.esri.arcgismaps.sample.playkmltour
import android.os.Bundleimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContentimport androidx.activity.enableEdgeToEdgeimport androidx.compose.material3.MaterialThemeimport androidx.compose.material3.Surfaceimport androidx.compose.runtime.Composableimport com.arcgismaps.ApiKeyimport com.arcgismaps.ArcGISEnvironmentimport com.esri.arcgismaps.sample.sampleslib.theme.SampleAppThemeimport com.esri.arcgismaps.sample.playkmltour.screens.PlayKmlTourScreen
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // authentication with an API key or named user is // required to access basemaps and other location services ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.ACCESS_TOKEN)
enableEdgeToEdge() setContent { SampleAppTheme { PlayKMLTourApp() } } }
@Composable private fun PlayKMLTourApp() { Surface(color = MaterialTheme.colorScheme.background) { PlayKmlTourScreen( sampleName = getString(R.string.play_kml_tour_app_name) ) } }}/* Copyright 2024 Esri * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */
package com.esri.arcgismaps.sample.playkmltour.components
import android.app.Applicationimport androidx.lifecycle.AndroidViewModelimport androidx.lifecycle.viewModelScopeimport com.arcgismaps.mapping.ArcGISSceneimport com.arcgismaps.mapping.ArcGISTiledElevationSourceimport com.arcgismaps.mapping.BasemapStyleimport com.arcgismaps.mapping.Surfaceimport com.arcgismaps.mapping.Viewpointimport com.arcgismaps.mapping.kml.KmlContainerimport com.arcgismaps.mapping.kml.KmlDatasetimport com.arcgismaps.mapping.kml.KmlNodeimport com.arcgismaps.mapping.kml.KmlTourimport com.arcgismaps.mapping.kml.KmlTourControllerimport com.arcgismaps.mapping.kml.KmlTourStatusimport com.arcgismaps.mapping.layers.KmlLayerimport com.arcgismaps.toolkit.geoviewcompose.SceneViewProxyimport com.esri.arcgismaps.sample.playkmltour.Rimport com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModelimport kotlinx.coroutines.flow.MutableStateFlowimport kotlinx.coroutines.flow.asStateFlowimport kotlinx.coroutines.flow.combineimport kotlinx.coroutines.launchimport java.io.File
class PlayKmlTourViewModel(application: Application) : AndroidViewModel(application) {
private val provisionPath: String by lazy { application.getExternalFilesDir(null)?.path.toString() + File.separator + application.getString(R.string.play_kml_tour_app_name) }
// add elevation data private val surface = Surface().apply { elevationSources.add(ArcGISTiledElevationSource("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")) }
// create a KML layer from a KML dataset with a KML tour private val kmlDataSet = KmlDataset(provisionPath + File.separator + "Esri_tour.kmz") private val kmlLayer = KmlLayer(kmlDataSet)
// create a scene with the surface and KML layer val arcGISScene = ArcGISScene(BasemapStyle.ArcGISImagery).apply { baseSurface = surface initialViewpoint = Viewpoint(39.8, -98.6, 10e7) operationalLayers.add(kmlLayer) }
val sceneViewProxy = SceneViewProxy()
private var kmlTour: KmlTour? = null private val kmlTourController = KmlTourController()
private val _kmlTourStatusFlow: MutableStateFlow<KmlTourStatus> = MutableStateFlow(KmlTourStatus.NotInitialized) val kmlTourStatusFlow = _kmlTourStatusFlow.asStateFlow()
private val _kmlTourProgressFlow: MutableStateFlow<Float> = MutableStateFlow(0.0f) val kmlTourProgressFlow = _kmlTourProgressFlow.asStateFlow()
// Create a message dialog view model for handling error messages val messageDialogVM = MessageDialogViewModel()
init { viewModelScope.launch { arcGISScene.load().onFailure { error -> messageDialogVM.showMessageDialog( "Failed to load scene", error.message.toString() ) } kmlLayer.load().onSuccess { findFirstKMLTour(kmlDataSet.rootNodes)?.let { kmlTour = it collectKmlTourStatus(it) kmlTourController.tour = it collectProgress(kmlTourController) } }.onFailure { error -> messageDialogVM.showMessageDialog( "Failed to load KML tour", error.message.toString() ) } } }
/** * Plays or pauses the KML tour */ fun playOrPause() { kmlTour?.let { when (it.status.value) { KmlTourStatus.Playing -> kmlTourController.pause() KmlTourStatus.Paused, KmlTourStatus.Initialized, KmlTourStatus.Completed -> kmlTourController.play() else -> throw IllegalStateException("KML tour is not initialized") } } }
/** * Resets the tour */ fun reset() { kmlTourController.reset() arcGISScene.initialViewpoint?.let { sceneViewProxy.setViewpoint(it) } }
/** * Collects the progress of the KML tour and puts it into a state flow */ private fun collectProgress(kmlTourController: KmlTourController) = viewModelScope.launch { kmlTourController.currentPosition.combine(kmlTourController.totalDuration) { currentPosition, totalDuration -> (currentPosition / totalDuration).toFloat() }.collect { progress -> _kmlTourProgressFlow.value = progress } }
/** * Collects the status of the KML tour and puts it into a state flow */ private fun collectKmlTourStatus(kmlTour: KmlTour) = viewModelScope.launch { kmlTour.status.collect { state -> _kmlTourStatusFlow.value = state } }
/** * Recursively searches for the first KML tour in a list of [kmlNodes]. * Returns the first [KmlTour], or null if there are no tours. */ private fun findFirstKMLTour(kmlNodes: List<KmlNode>): KmlTour? { kmlNodes.forEach { node -> if (node is KmlTour) return node else if (node is KmlContainer) return findFirstKMLTour(node.childNodes) }
return null }}/* Copyright 2024 Esri * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */
package com.esri.arcgismaps.sample.playkmltour.screens
import androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.paddingimport androidx.compose.material3.Buttonimport androidx.compose.material3.Iconimport androidx.compose.material3.LinearProgressIndicatorimport androidx.compose.material3.Scaffoldimport androidx.compose.material3.Textimport androidx.compose.runtime.Composableimport androidx.compose.runtime.getValueimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.res.painterResourceimport androidx.compose.ui.res.stringResourceimport androidx.compose.ui.unit.dpimport androidx.lifecycle.compose.collectAsStateWithLifecycleimport androidx.lifecycle.viewmodel.compose.viewModelimport com.arcgismaps.mapping.kml.KmlTourStatusimport com.arcgismaps.toolkit.geoviewcompose.SceneViewimport com.esri.arcgismaps.sample.playkmltour.Rimport com.esri.arcgismaps.sample.playkmltour.components.PlayKmlTourViewModelimport com.esri.arcgismaps.sample.sampleslib.components.MessageDialogimport com.esri.arcgismaps.sample.sampleslib.components.SampleTopAppBar
/** * Main screen layout for the sample */@Composablefun PlayKmlTourScreen(sampleName: String) { val viewModel: PlayKmlTourViewModel = viewModel()
val kmlTourStatus by viewModel.kmlTourStatusFlow.collectAsStateWithLifecycle() val kmlTourProgress by viewModel.kmlTourProgressFlow.collectAsStateWithLifecycle()
Scaffold( topBar = { SampleTopAppBar(title = sampleName) }, content = { Column( modifier = Modifier .fillMaxSize() .padding(it), ) { SceneView( modifier = Modifier .fillMaxSize() .weight(1f), isAttributionBarVisible = false, arcGISScene = viewModel.arcGISScene, sceneViewProxy = viewModel.sceneViewProxy )
val padding = 8.dp
LinearProgressIndicator(progress = { kmlTourProgress }, modifier = Modifier .fillMaxWidth() .padding(all = padding)) Text("${stringResource(R.string.tour_status)} ${tourStateToString(kmlTourStatus)}", modifier = Modifier .padding(all = padding) .align(Alignment.CenterHorizontally)) Row(modifier = Modifier.fillMaxWidth()) { Button(enabled = (kmlTourStatus == KmlTourStatus.Paused || kmlTourStatus == KmlTourStatus.Playing), modifier = Modifier .fillMaxWidth(0.5f) .padding(all = padding), onClick = { viewModel.reset() }) { Icon( painter = painterResource(id = R.drawable.ic_baseline_reset_24), contentDescription = null ) Text(stringResource(R.string.reset)) } Button(enabled = (kmlTourStatus != KmlTourStatus.NotInitialized), modifier = Modifier .fillMaxWidth() .padding(all = padding), onClick = { // play tour viewModel.playOrPause() }) { if (kmlTourStatus == KmlTourStatus.Playing) { Icon( painter = painterResource(id = R.drawable.ic_round_pause_24), contentDescription = null ) Text(stringResource(R.string.pause)) } else { Icon( painter = painterResource(id = R.drawable.ic_round_play_arrow_24), contentDescription = null ) Text(stringResource(R.string.play)) } } } }
viewModel.messageDialogVM.apply { if (dialogStatus) { MessageDialog( title = messageTitle, description = messageDescription, onDismissRequest = ::dismissDialog ) } } } )}
private fun tourStateToString(tourStatus: KmlTourStatus) : String { return when (tourStatus) { KmlTourStatus.Paused -> "Paused" KmlTourStatus.Playing -> "Playing" KmlTourStatus.Completed -> "Completed" KmlTourStatus.Initializing -> "Initializing" KmlTourStatus.Initialized -> "Initialized" KmlTourStatus.NotInitialized -> "Not Initialized" }}