Play KML tour

View on GitHubSample viewer app

Play tours in KML files.

Image of play KML tour

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

  1. Create a KmlDataSet from the local kmz file and instantiate a layer from it with KmlLayer(kmlDataSet).
  2. Create the KML tour controller. Wire up the buttons to the KmlTourController.play(), KmlTourController.pause(), and KmlTourController.reset() methods.
  3. 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

PlayKmlTourViewModel.ktPlayKmlTourViewModel.ktDownloadActivity.ktMainActivity.ktPlayKmlTourScreen.kt
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* 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
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.

OSZAR »