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. ArcGIS Maps SDK for Java 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. Click the play button to start the tour. The narration audio will start and then the viewpoint will animate. Press the button again to pause the tour. To restart the tour, hit the refresh button and then the play button.

How it works

  1. Create a KmlDataset with the path to a local KML file with a KML tour.
  2. Create and load a KmlLayer with the dataset.
  3. When the layer has loaded, search its KmlNodes by recursing through kmlLayer.getRootNodes() to find a KmlTour node.
  4. Create a KmlTourController and set the tour with kmlTourController.setTour(kmlTour).
  5. Use kmltourController.play(), kmltourController.pause(), and kmltourController.reset() to control the tour.

Relevant API

  • KmlTour
  • KmlTourController

About the data

This sample uses a custom tour created by the ArcGIS Maps SDKs for Native Apps 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

PlayAKMLTourSample.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
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
204
205
206
207
208
209
210
211
/*
 * 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.samples.play_a_kml_tour;

import java.io.File;
import java.util.List;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

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.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;
import com.esri.arcgisruntime.ogc.kml.KmlTourStatus;

public class PlayAKMLTourSample extends Application {

  private KmlTourController kmlTourController;
  private SceneView sceneView;

  @Override
  public void start(Stage stage) {
    try {
      // create stack pane and application scene
      StackPane stackPane = new StackPane();
      Scene fxScene = new Scene(stackPane);

      // set up the stage
      stage.setTitle("Play a KML Tour Sample");
      stage.setWidth(800);
      stage.setHeight(700);
      stage.setScene(fxScene);
      stage.show();

      // authentication with an API key or named user is required to access basemaps and other location services
      String yourAPIKey = System.getProperty("apiKey");
      ArcGISRuntimeEnvironment.setApiKey(yourAPIKey);

      // create a scene and show it in a scene view
      sceneView = new SceneView();
      ArcGISScene scene = new ArcGISScene(BasemapStyle.ARCGIS_IMAGERY);
      sceneView.setArcGISScene(scene);

      // add elevation data
      Surface surface = new Surface();
      surface.getElevationSources().add(new ArcGISTiledElevationSource("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"));
      scene.setBaseSurface(surface);

      // create play/pause button
      final ImageView playIcon = new ImageView(new Image(PlayAKMLTourSample.class.getResourceAsStream(
          "/play.png")));
      final ImageView pauseIcon = new ImageView(new Image(PlayAKMLTourSample.class.getResourceAsStream(
          "/pause.png")));

      Button playPauseButton = new Button();
      playPauseButton.setGraphic(playIcon);
      playPauseButton.setDisable(true);

      playPauseButton.setOnAction(e -> {
        if (kmlTourController.getTour().getTourStatus() == KmlTourStatus.PLAYING) {
          kmlTourController.pause();
        } else {
          kmlTourController.play();
        }
      });

      // create replay button
      final ImageView replayIcon = new ImageView(new Image(PlayAKMLTourSample.class.getResourceAsStream(
          "/replay.png")));

      Button replayButton = new Button();
      replayButton.setGraphic(replayIcon);
      replayButton.setDisable(true);

      replayButton.setOnAction(e -> {
        kmlTourController.reset();
        playPauseButton.setGraphic(playIcon);
        playPauseButton.setDisable(false);
      });

      VBox controlsVBox = new VBox(6);
      controlsVBox.setMaxSize(50, 100);
      controlsVBox.getChildren().addAll(playPauseButton, replayButton);

      // add a KML layer from a KML dataset with a KML tour
      KmlDataset kmlDataset = new KmlDataset(new File(System.getProperty("data.dir"), "./samples-data/kml/Esri_tour.kmz").getAbsolutePath());
      KmlLayer kmlLayer = new KmlLayer(kmlDataset);
      scene.getOperationalLayers().add(kmlLayer);

      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) {
            // enable/disable buttons based on the KML tour status
            kmlTour.addStatusChangedListener(kmlTourStatusChangedEvent -> {
              switch (kmlTourStatusChangedEvent.getStatus()) {
                case NOT_INITIALIZED:
                  // before kml tour is added to controller
                case INITIALIZING:
                  // when kml tour is added to controller
                  playPauseButton.setDisable(true);
                  replayButton.setDisable(true);
                  break;
                case INITIALIZED:
                  // when kml tour is ready to be played
                  playPauseButton.setDisable(false);
                  replayButton.setDisable(false);
                  break;
                case PAUSED:
                  playPauseButton.setGraphic(playIcon);
                  break;
                case PLAYING:
                  playPauseButton.setGraphic(pauseIcon);
                  break;
                case COMPLETED:
                  playPauseButton.setDisable(true);
                  break;
              }
            });

            // set the tour to the tour controller
            kmlTourController = new KmlTourController();
            kmlTourController.setTour(kmlTour);
          } else {
            new Alert(Alert.AlertType.WARNING, "No KML tour found in dataset").show();
          }
        } else {
          new Alert(Alert.AlertType.ERROR, "KML Layer failed to load").show();
        }
      });

      stackPane.getChildren().addAll(sceneView, controlsVBox);
      StackPane.setMargin(controlsVBox, new Insets(10));
      StackPane.setAlignment(controlsVBox, Pos.TOP_LEFT);
    } catch (Exception ex) {
      // on any exception, print the stack trace
      ex.printStackTrace();
    }
  }

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

  /**
   * Stops and releases all resources used in application.
   */
  @Override
  public void stop() {

    if (sceneView != null) {
      sceneView.dispose();
    }
  }

  /**
   * Opens and runs application.
   *
   * @param args arguments passed to this application
   */
  public static void main(String[] args) {

    Application.launch(args);
  }
}

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