Play KML tour

View 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. The Maps SDKs allow 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 the play and pause button 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. Load the KMLDataset 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 and find a 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.Status
  • 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 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

PlayKMLTourView.swift
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
// Copyright 2023 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
//
//   https://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.

import ArcGIS
import SwiftUI

struct PlayKMLTourView: View {
    /// A scene with an imagery basemap and a world elevation service.
    @State private var scene: ArcGIS.Scene = {
        let scene = Scene(basemapStyle: .arcGISImagery)
        scene.initialViewpoint = Viewpoint(latitude: .nan, longitude: .nan, scale: 114_145_911)

        // Set the scene's base surface with an elevation source.
        let surface = Surface()
        let elevationSource = ArcGISTiledElevationSource(url: .worldElevationService)
        surface.addElevationSource(elevationSource)
        scene.baseSurface = surface

        return scene
    }()

    /// The current viewpoint of the scene view used when reseting the tour.
    @State private var viewpoint: Viewpoint?

    /// The tour controller used to control the KML tour.
    @State private var tourController = KMLTourController()

    /// The current status of the KML tour.
    @State private var tourStatus: KMLTour.Status = .notInitialized

    /// The error shown in the error alert.
    @State private var error: Error?

    /// A Boolean value that indicates whether to disable the tour buttons.
    private var tourDisabled: Bool {
        tourStatus == .notInitialized || tourStatus == .initializing
    }

    var body: some View {
        // Create a scene view with a scene and a viewpoint.
        SceneView(scene: scene, viewpoint: viewpoint)
            .task {
                do {
                    // Add KML layer to the scene.
                    let dataset = KMLDataset(url: .esriTour)
                    try await dataset.load()
                    let layer = KMLLayer(dataset: dataset)
                    scene.addOperationalLayer(layer)

                    // Set the tour controller with first tour in the dataset.
                    tourController.tour = dataset.tours.first

                    // Listen for tour status updates.
                    if let tour = tourController.tour {
                        for await status in tour.$status {
                            tourStatus = status
                        }
                    }
                } catch {
                    self.error = error
                }
            }
            .onDisappear {
                tourController.pause()
            }
            .toolbar {
                ToolbarItemGroup(placement: .bottomBar) {
                    Button {
                        tourController.reset()
                        viewpoint = scene.initialViewpoint
                    } label: {
                        Image(systemName: "gobackward")
                    }
                    .disabled(tourDisabled || tourStatus == .initialized)
                    Spacer()
                    Button {
                        tourStatus == .playing ? tourController.pause() : tourController.play()
                    } label: {
                        Image(systemName: tourStatus == .playing ? "pause.fill" : "play.fill")
                    }
                    .disabled(tourDisabled)
                    Spacer()
                }
            }
            .overlay(alignment: .top) {
                Text("Tour status: \(String(describing: tourStatus).titleCased)")
                    .frame(maxWidth: .infinity, alignment: .center)
                    .padding(8)
                    .background(.thinMaterial, ignoresSafeAreaEdges: .horizontal)
            }
            .overlay(alignment: .center) {
                if tourDisabled {
                    ProgressView()
                        .padding()
                        .background(.ultraThickMaterial)
                        .cornerRadius(10)
                        .shadow(radius: 50)
                }
            }
            .errorAlert(presentingError: $error)
    }
}

private extension KMLDataset {
    /// All the tours in the dataset.
    var tours: [KMLTour] { rootNodes.tours }
}

private extension KMLContainer {
    /// All the tours in the container.
    var tours: [KMLTour] { childNodes.tours }
}

private extension Sequence where Element == KMLNode {
    /// All the tours in the node sequence.
    var tours: [KMLTour] {
        reduce(into: []) { tours, node in
            switch node {
            case let tour as KMLTour:
                tours.append(tour)
            case let container as KMLContainer:
                tours.append(contentsOf: container.tours)
            default:
                break
            }
        }
    }
}

private extension String {
    // A copy of a camel cased string broken into words with capital letters.
    var titleCased: String {
        self
            .replacingOccurrences(of: "([A-Z])", with: " $1", options: .regularExpression)
            .trimmingCharacters(in: .whitespacesAndNewlines)
            .capitalized
    }
}

private extension URL {
    /// A URL to world elevation service from Terrain3D ArcGIS REST service.
    static var worldElevationService: URL {
        URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
    }

    /// A URL to a KMZ file of an Esri Tour on ArcGIS Online.
    static var esriTour: URL {
        URL(string: "https://www.arcgis.com/sharing/rest/content/items/f10b1d37fdd645c9bc9b189fb546307c/data")!
    }
}

#Preview {
    NavigationView {
        PlayKMLTourView()
    }
}

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