Play KML tour

View inMAUIUWPWPFWinUIView on GitHub

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. Use the Reset button to return the tour to the unplayed state.

This sample is graphics intensive and includes audio. Performance may be poor on emulated and remote devices. Run the sample on a physical device for best performance.

How it works

  1. Load the KML file and add it to a layer.
  2. Create the KML tour controller. Wire up the buttons to the PlayPause and 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.
  4. Enable the buttons to allow the user to play, pause, and reset the tour.

Relevant API

  • KmlTour
  • KmlTour.PropertyChanged
  • KmlTour.TourStatus
  • KmlTourController
  • KmlTourController.Pause()
  • KmlTourController.Play()
  • KmlTourController.Reset()
  • KmlTourController.Tour

Offline data

Data will be downloaded by the sample viewer automatically.

About the data

This sample uses a custom tour created by a member of Esri. When you play the tour, you'll see a journey through some of Esri's offices.

Additional information

See Touring in KML in Keyhole Markup Language for more information.

Tags

animation, interactive, KML, narration, pause, play, story, tour

Sample Code

PlayKmlTours.xaml.csPlayKmlTours.xaml.csPlayKmlTours.xaml
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2022 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.

using ArcGIS.Samples.Managers;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Ogc;
using Esri.ArcGISRuntime.UI;
using System.ComponentModel;
using System.Diagnostics;

namespace ArcGIS.Samples.PlayKmlTours
{
    [ArcGIS.Samples.Shared.Attributes.Sample(
        name: "Play KML tour",
        category: "Layers",
        description: "Play tours in KML files.",
        instructions: "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. Use the _Reset_ button to return the tour to the unplayed state.",
        tags: new[] { "KML", "animation", "interactive", "narration", "pause", "play", "story", "tour" })]
    [ArcGIS.Samples.Shared.Attributes.OfflineData("f10b1d37fdd645c9bc9b189fb546307c")]
    public partial class PlayKmlTours : ContentPage, IDisposable
    {
        // The KML tour controller provides player controls for KML tours.
        private readonly KmlTourController _tourController = new KmlTourController();

        // Keep track of play/pause status.
        private bool _tourPlaying;

        public PlayKmlTours()
        {
            InitializeComponent();
            _ = Initialize();
        }

        private async Task Initialize()
        {
            // Load the scene with a basemap and a terrain surface.
            MySceneView.Scene = new Scene(BasemapStyle.ArcGISImageryStandard);
            MySceneView.Scene.BaseSurface.ElevationSources.Add(new ArcGISTiledElevationSource(new Uri("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")));

            // Get the URL to the data.
            string filePath = DataManager.GetDataFolder("f10b1d37fdd645c9bc9b189fb546307c", "Esri_tour.kmz");
            Uri kmlUrl = new Uri(filePath);

            // Create the KML dataset and layer.
            KmlDataset dataset = new KmlDataset(kmlUrl);
            KmlLayer layer = new KmlLayer(dataset);

            // Add the layer to the map.
            MySceneView.Scene.OperationalLayers.Add(layer);

            try
            {
                // Load the dataset.
                await dataset.LoadAsync();

                // Find the first KML tour.
                FindKmlTour(dataset.RootNodes);

                // Handle absence of tour gracefully.
                if (_tourController.Tour == null)
                {
                    throw new InvalidOperationException("No tour found. Can't enable touring for a KML file with no tours.");
                }

                // Listen for changes to the tour status.
                _tourController.Tour.PropertyChanged += Tour_PropertyChanged;

                // Enable the play button.
                PlayPauseButton.IsEnabled = true;

                // Hide the status bar.
                LoadingStatusBar.IsVisible = false;

                // Create scene interaction options which will be disabled when the tour begins.
                MySceneView.InteractionOptions = new SceneViewInteractionOptions
                {
                    IsEnabled = false
                };
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                await Application.Current.MainPage.DisplayAlert("Error", e.ToString(), "OK");
            }
        }

        private void FindKmlTour(IEnumerable<KmlNode> rootNodes)
        {
            // Hold a list of nodes to explore.
            Queue<KmlNode> nodesToExplore = new Queue<KmlNode>();

            // Add each root node to the list.
            foreach (KmlNode rootNode in rootNodes)
            {
                nodesToExplore.Enqueue(rootNode);
            }

            // Keep exploring until a tour is found or there are no more nodes.
            while (nodesToExplore.Any())
            {
                // Remove a node from the queue.
                KmlNode currentNode = nodesToExplore.Dequeue();

                // If the node is a tour, use it.
                if (currentNode is KmlTour tour)
                {
                    _tourController.Tour = tour;
                    return;
                }

                // If the node is a container, add all of its children to the list of nodes to explore.
                if (currentNode is KmlContainer container)
                {
                    foreach (KmlNode node in container.ChildNodes)
                    {
                        nodesToExplore.Enqueue(node);
                    }
                }

                // Otherwise, continue.
            }
        }

        private void Tour_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            // Skip for everything except tour status property changes.
            if (e.PropertyName != nameof(KmlTour.TourStatus))
            {
                return;
            }

            // Set the UI based on the current state of the tour.
            switch (_tourController.Tour.TourStatus)
            {
                case KmlTourStatus.Completed:
                    PlayPauseButton.IsEnabled = true;
                    ResetButton.IsEnabled = false;
                    PlayPauseButton.Text = "Play";
                    _tourPlaying = false;
                    MySceneView.InteractionOptions.IsEnabled = true;

                    // Return to the initial viewpoint to visually indicate the tour being over.
                    MySceneView.SetViewpointAsync(MySceneView.Scene.InitialViewpoint);
                    break;

                case KmlTourStatus.Initialized:
                    PlayPauseButton.IsEnabled = true;
                    ResetButton.IsEnabled = false;
                    PlayPauseButton.Text = "Play";
                    _tourPlaying = false;
                    MySceneView.InteractionOptions.IsEnabled = true;
                    break;

                case KmlTourStatus.Playing:
                    ResetButton.IsEnabled = true;
                    PlayPauseButton.IsEnabled = true;
                    PlayPauseButton.Text = "Pause";
                    _tourPlaying = true;
                    MySceneView.InteractionOptions.IsEnabled = false;
                    break;

                case KmlTourStatus.Paused:
                    PlayPauseButton.Text = "Play";
                    _tourPlaying = false;
                    break;
            }
        }

        // Play and pause the tour when the button is pressed.
        private void PlayPause_Clicked(object sender, EventArgs e)
        {
            if (_tourPlaying)
            {
                _tourController?.Pause();
            }
            else
            {
                _tourController?.Play();
            }
        }

        // Reset the tour when the button is pressed.
        private void Reset_Clicked(object sender, EventArgs e)
        {
            _tourController?.Reset();
            MySceneView.SetViewpoint(MySceneView.Scene.InitialViewpoint);
            PlayPauseButton.Text = "Play";
        }

        public void Dispose()
        {
            // Reset the tour when the user leaves the sample.
            _tourController?.Pause();
            _tourController?.Reset();
        }
    }
}

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