Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Play KML Tour

This code sample is available for these platforms:
View Sample 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. When you're ready to show 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 Play, Pause, 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 the ArcGIS Runtime SDK samples team. When you play the tour, you'll see a narrated journey through some of Esri's offices.

Additional information

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


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

Sample Code

<ContentPage xmlns=""
        <esriUI:SceneView x:Name="MySceneView" Style="{StaticResource SceneWithFormStyle}"
                        BindingContext="{x:Reference Name=ResponsiveFormContainer}" />
        <resources:ResponsiveFormContainer x:Name="ResponsiveFormContainer">
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                <Label Text="Use the buttons to control the tour. Contains audio. 🎧"
                           Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
                <Button x:Name="PlayButton"
                        Grid.Row="1" Grid.Column="0"
                        Clicked="Play_Clicked" />
                <Button x:Name="PauseButton"
                        Grid.Row="1" Grid.Column="1"
                        Clicked="Pause_Clicked" />
                <Button x:Name="ResetButton"
                        Grid.Row="1" Grid.Column="2"
                        Clicked="Reset_Clicked" />
                <ActivityIndicator x:Name="LoadingStatusBar"
                                   Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3"
                                   IsEnabled="True" IsRunning="True"  />
// Copyright 2019 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:
// 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 ArcGISRuntime.Samples.Managers;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Ogc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Xamarin.Forms;

namespace ArcGISRuntimeXamarin.Samples.PlayKmlTours
        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. When you're ready to show the tour, use the reset button to return the tour to the unplayed state.",
        tags: new[] { "KML", "animation", "interactive", "narration", "pause", "play", "story", "tour" })]
    public partial class PlayKmlTours : ContentPage, IDisposable
        // The KML tour controller provides player controls for KML tours.
        private readonly KmlTourController _tourController = new KmlTourController();

        public PlayKmlTours()

        private async void Initialize()
            // Load the scene with a basemap and a terrain surface.
            MySceneView.Scene = new Scene(Basemap.CreateImagery());
            MySceneView.Scene.BaseSurface.ElevationSources.Add(new ArcGISTiledElevationSource(new Uri("")));

            // 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.

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

                // Find the first KML tour.

                // 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.
                PlayButton.IsEnabled = true;

                // Hide the status bar.
                LoadingStatusBar.IsVisible = false;
            catch (Exception e)
                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)

            // 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;

                // 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)

                // Otherwise, continue.

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

            // Set the UI based on the current state of the tour.
            switch (_tourController.Tour.TourStatus)
                case KmlTourStatus.Completed:
                case KmlTourStatus.Initialized:
                    PlayButton.IsEnabled = true;
                    PauseButton.IsEnabled = false;
                case KmlTourStatus.Paused:
                    PlayButton.IsEnabled = true;
                    PauseButton.IsEnabled = false;
                    ResetButton.IsEnabled = true;
                case KmlTourStatus.Playing:
                    ResetButton.IsEnabled = true;
                    PlayButton.IsEnabled = false;
                    PauseButton.IsEnabled = true;

        // Play the tour when the button is pressed.
        private void Play_Clicked(object sender, EventArgs e) => _tourController?.Play();

        // Pause the tour when the button is pressed.
        private void Pause_Clicked(object sender, EventArgs e) => _tourController?.Pause();

        // Reset the tour when the button is pressed.
        private void Reset_Clicked(object sender, EventArgs e) => _tourController?.Reset();

        public void Dispose()
            // Reset the tour when the user leaves the sample.