Play tours in KML files.
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
- Create a
KmlDataSet
from the local kmz file and instantiate a layer from it withnew KmlLayer(kmlDataSet)
. - Create the KML tour controller. Wire up the buttons to the
kmlController.play()
,kmlController.pause()
, andkmlController.reset()
methods. - 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
- Download the data from ArcGIS Online.
- Open your command prompt and navigate to the folder where you extracted the contents of the data from step 1.
- 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
/*
* 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();
}
}