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
KmlDataSet
from 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.components
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
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.kml.KmlContainer
import com.arcgismaps.mapping.kml.KmlDataset
import com.arcgismaps.mapping.kml.KmlNode
import com.arcgismaps.mapping.kml.KmlTour
import com.arcgismaps.mapping.kml.KmlTourController
import com.arcgismaps.mapping.kml.KmlTourStatus
import com.arcgismaps.mapping.layers.KmlLayer
import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
import com.esri.arcgismaps.sample.playkmltour.R
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import 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
}
}