Play KML tour

View on GitHub
Sample 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 new KmlLayer(kmlDataSet).
  2. Create the KML tour controller. Wire up the buttons to the, kmlController.pause(), and kmlController.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.reset()

Offline Data

  1. Download the data from ArcGIS Online.
  2. Open your command prompt and navigate to the folder where you extracted the contents of the data from step 1.
  3. Push the data into the scoped storage of the sample app: adb push Esri_tour.kmz /Android/data/com.esri.arcgisruntime.sample.playkmltour/files/Esri_tour.kmz

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

package com.esri.arcgisruntime.sample.playkmltour;

import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatImageButton;
import com.esri.arcgisruntime.layers.KmlLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISScene;
import com.esri.arcgisruntime.mapping.ArcGISTiledElevationSource;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.Surface;
import com.esri.arcgisruntime.mapping.view.SceneView;
import com.esri.arcgisruntime.ogc.kml.KmlContainer;
import com.esri.arcgisruntime.ogc.kml.KmlDataset;
import com.esri.arcgisruntime.ogc.kml.KmlNode;
import com.esri.arcgisruntime.ogc.kml.KmlTour;
import com.esri.arcgisruntime.ogc.kml.KmlTourController;

public class MainActivity extends AppCompatActivity {

  private static final String TAG = MainActivity.class.getSimpleName();

  private SceneView mSceneView;
  private KmlTourController mKmlTourController;

  protected void onCreate(Bundle savedInstanceState) {

    mSceneView = findViewById(;

    // create the controller used to play, pause and reset the tour
    mKmlTourController = new KmlTourController(this);

    // create a scene
    ArcGISScene scene = new ArcGISScene(Basemap.createImagery());

    // add elevation data
    Surface surface = new Surface();
    surface.getElevationSources().add(new ArcGISTiledElevationSource(getString(R.string.world_terrain_service)));

    // add a KML layer from a KML dataset with a KML tour
    KmlDataset kmlDataset = new KmlDataset(getExternalFilesDir(null) + getString(R.string.kml_tour_path));
    KmlLayer kmlLayer = new KmlLayer(kmlDataset);

    // handle play click, not enabled until kml layer has loaded
    AppCompatImageButton playButton = findViewById(;
    playButton.setOnClickListener(v ->;

    // handle pause click, not enabled until kml layer has loaded
    AppCompatImageButton pauseButton = findViewById(;
    pauseButton.setOnClickListener(v -> mKmlTourController.pause());

    // handle reset click, not enabled until kml layer has loaded
    AppCompatImageButton resetButton = findViewById(;
    resetButton.setOnClickListener(v -> mKmlTourController.reset());
    kmlLayer.addDoneLoadingListener(() -> {
      if (kmlLayer.getLoadStatus() == LoadStatus.LOADED) {
        // find the first KML tour in the dataset when loaded
        KmlTour kmlTour = findFirstKMLTour(kmlDataset.getRootNodes());
        if (kmlTour != null) {
          // set the tour to the tour controller and enable UI controls
        } else {
          String error = "No KML tour found in dataset";
          Toast.makeText(this, error, Toast.LENGTH_LONG).show();
          Log.e(TAG, error);
      } else {
        String error = "KML layer failed to load: " + kmlLayer.getLoadError().getMessage();
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
        Log.e(TAG, error);

   * Recursively searches for the first KML tour in a list of KML nodes.
   * @return the first KML tour, or null if there are no tours
  private static KmlTour findFirstKMLTour(List<KmlNode> kmlNodes) {
    for (KmlNode node : kmlNodes) {
      if (node instanceof KmlTour) {
        return (KmlTour) node;
      } else if (node instanceof KmlContainer) {
        return findFirstKMLTour(((KmlContainer) node).getChildNodes());
    return null;

  protected void onPause() {

  protected void onResume() {

  protected void onDestroy() {