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 new KmlLayer(kmlDataSet).
  2. Create the KML tour controller. Wire up the buttons to the kmlController.play(), 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.play()
  • 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 API 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.

Tags

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

Sample Code

MainActivity.java
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
/*
 *  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
 *
 *     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.arcgisruntime.sample.playkmltour;

import java.util.List;

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

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageButton;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
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.BasemapStyle;
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;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // authentication with an API key or named user is required to access basemaps and other
    // location services
    ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY);

    mSceneView = findViewById(R.id.sceneView);

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

    // create a scene
    ArcGISScene scene = new ArcGISScene(BasemapStyle.ARCGIS_IMAGERY);
    mSceneView.setScene(scene);

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

    // 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);
    mSceneView.getScene().getOperationalLayers().add(kmlLayer);

    // handle play click, not enabled until kml layer has loaded
    AppCompatImageButton playButton = findViewById(R.id.playButton);
    playButton.setOnClickListener(v -> mKmlTourController.play());
    playButton.setEnabled(false);

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

    // handle reset click, not enabled until kml layer has loaded
    AppCompatImageButton resetButton = findViewById(R.id.resetButton);
    resetButton.setOnClickListener(v -> mKmlTourController.reset());
    resetButton.setEnabled(false);
    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
          mKmlTourController.setTour(kmlTour);
          playButton.setEnabled(true);
          pauseButton.setEnabled(true);
          resetButton.setEnabled(true);
        } 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;
  }

  @Override
  protected void onPause() {
    mSceneView.pause();
    super.onPause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mSceneView.resume();
  }

  @Override
  protected void onDestroy() {
    mSceneView.dispose();
    super.onDestroy();
  }
}

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