View on GitHub

List the contents of a KML file.

Image of list contents of KML file

Use case

KML files can contain a hierarchy of features, including network links to other KML content. A user may wish to traverse through the contents of KML nodes to know what data is contained within each node and, recursively, their children.

How to use the sample

The contents of the KML file are shown in a tree. Select a node to zoom to that node. Not all nodes can be zoomed to (e.g. screen overlays).

How it works

  1. Add the KML file to the scene as a layer.
  2. Explore the root nodes of the KmlDataset recursively explored to create a view model.
  • Each node is enabled for display at this step. KML files may include nodes that are turned off by default.
  1. When a node is selected, use the node’s extent to determine a viewpoint and set the ArcGISSceneView object’s viewpoint to it.

Relevant API

  • KmlContainer
  • KmlDataset
  • KmlDocument
  • KmlFolder
  • KmlGroundOverlay
  • KmlLayer
  • KmlNetworkLink
  • KmlNode
  • KmlPhotoOverlay
  • KmlPlacemark
  • KmlScreenOverlay

Tags

Keyhole, KML, KMZ, layers, OGC

Sample code

list_contents_of_kml_file.dart
// Copyright 2026 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 'dart:io';
import 'package:arcgis_maps/arcgis_maps.dart';
import 'package:arcgis_maps_sdk_flutter_samples/common/common.dart';
import 'package:go_router/go_router.dart';
import 'package:material_ui/material_ui.dart';
class ListContentsOfKmlFile extends StatefulWidget {
const ListContentsOfKmlFile({super.key});
@override
State<ListContentsOfKmlFile> createState() => _ListContentsOfKmlFileState();
}
class _ListContentsOfKmlFileState extends State<ListContentsOfKmlFile>
with SampleStateSupport {
// Create a controller for the scene view.
final _sceneViewController = ArcGISSceneView.createController();
// Dataset for the data in KML file.
late final KmlDataset _kmlDataset;
// The KML document containing the nodes to list.
KmlDocument? _kmlDocument;
// Flag to indicate if the bottom sheet should be shown.
var _showBottomSheet = true;
// Listing of parent nodes of the selected KML node. List includes selected
// node. Used to expand list tiles to show selected node.
var _selectedNodeAncestors = <KmlNode>[];
// A flag for when the scene view is ready and controls can be used.
var _ready = false;
@override
void initState() {
super.initState();
_kmlDataset = _initKmlDataset();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
left: false,
right: false,
child: Stack(
children: [
Column(
children: [
Expanded(
// Add a scene view to the widget tree and set a controller.
child: ArcGISSceneView(
controllerProvider: () => _sceneViewController,
onSceneViewReady: onSceneViewReady,
),
),
// Button to show the bottom sheet.
ElevatedButton(
onPressed: () => setState(() => _showBottomSheet = true),
child: const Text('Show KML contents'),
),
],
),
// Display a progress indicator and prevent interaction until state is ready.
LoadingIndicator(visible: !_ready),
],
),
),
bottomSheet: _showBottomSheet
? BottomSheetSettings(
title: 'KML Contents',
onCloseIconPressed: () =>
setState(() => _showBottomSheet = false),
settingsWidgets: (context) => [
const Column(
children: [
Text('Expand the KML folders to view child nodes.'),
Text('Tap on the nodes to view them in a scene.'),
],
),
if (_kmlDocument != null)
ExpansionTile(
title: const Text('Document'),
initiallyExpanded: true,
childrenPadding: const EdgeInsets.only(left: 16),
children: _kmlDocument!.childNodes
.map(_buildKmlNode)
.toList(),
)
else
const Center(child: Text('KML dataset loading...')),
],
)
: null,
);
}
Future<void> onSceneViewReady() async {
// Create a scene with an imagery basemap style and add it to the scene view.
final scene = ArcGISScene.withBasemapStyle(.arcGISImagery);
_sceneViewController.arcGISScene = scene;
// Add a surface to the scene based on elevation data.
scene.baseSurface.elevationSources.add(
ArcGISTiledElevationSource.withUri(
Uri.parse(
'https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer',
),
),
);
// Create a KML layer and add it to the scene.
await _kmlDataset.load();
final kmlDocument = _kmlDataset.rootNodes.first as KmlDocument;
final kmlLayer = KmlLayer(_kmlDataset);
scene.operationalLayers.add(kmlLayer);
// Check if the widget is still mounted after the await before continuing.
if (!mounted) return;
// Set the ready state variable to true to enable the sample UI.
setState(() {
_kmlDocument = kmlDocument;
_ready = true;
});
}
// Function to initialize a KmlDataset based on the sample's KML file.
KmlDataset _initKmlDataset() {
final listPaths = GoRouter.of(context).state.extra! as List<String>;
final kmlFile = File(listPaths.first);
final kmlDataset = KmlDataset(kmlFile.uri);
return kmlDataset;
}
// Build function for KML leaf nodes. Clicking on these will view the item on the scene view.
Widget _buildKmlNode(KmlNode kmlNode) {
// Ensure node is visible.
kmlNode.isVisible = true;
if (kmlNode is KmlContainer) {
return ExpansionTile(
// Add an onTap GestureDetector to the text so users can view the folder's
// viewpoint while still being able to expand the tile.
title: GestureDetector(
onTap: () => _onKmlNodeSelected(kmlNode),
child: Text(kmlNode.name),
),
subtitle: Text(_getFriendlyTypeName(kmlNode)),
initiallyExpanded: _selectedNodeAncestors.contains(kmlNode),
childrenPadding: const EdgeInsets.only(left: 16),
children: kmlNode.childNodes.map(_buildKmlNode).toList(),
);
}
return ListTile(
title: Text(kmlNode.name),
subtitle: Text(_getFriendlyTypeName(kmlNode)),
onTap: () => _onKmlNodeSelected(kmlNode).ignore(),
);
}
String _getFriendlyTypeName(KmlNode kmlNode) {
return kmlNode.runtimeType
.toString()
.replaceFirst(RegExp('^Kml'), '')
.replaceAllMapped(
RegExp('[A-Z][a-z]*'),
(match) => ' ${match.group(0)}',
)
.trim();
}
// Function called when a leaf node is tapped. Sets viewpoint for the selected KML node.
Future<void> _onKmlNodeSelected(KmlNode kmlNode) async {
Viewpoint? nodeViewpoint;
// Record which node has been selected and the node's ancestry.
_selectedNodeAncestors = _buildAncestorNodeList(kmlNode);
final surface = _sceneViewController.arcGISScene?.baseSurface;
if (surface != null) {
nodeViewpoint = await _createViewpointForKmlNode(kmlNode, surface);
if (!mounted) return;
}
if (nodeViewpoint != null) {
// Change the viewpoint to show the item on the scene.
_sceneViewController.setViewpointAnimated(nodeViewpoint, duration: 1);
// Hide the bottom sheet.
setState(() => _showBottomSheet = false);
} else {
// Nothing to show, so alert the user.
showAlertDialog(
context,
'This node has no viewpoint or extent to view.',
showOK: true,
).ignore();
}
}
// Function to create a Viewpoint based on a KML node.
Future<Viewpoint?> _createViewpointForKmlNode(
KmlNode kmlNode,
Surface surface,
) async {
// Ensure the surface for the scene is loaded.
if (surface.loadStatus != .loaded) {
await surface.load();
}
final kmlViewpoint = kmlNode.viewpoint;
if (kmlViewpoint != null) {
// The node has a KML viewpoint. Get an ArcGIS Viewpoint from the KML viewpoint.
return await _createViewpointWithKmlViewpoint(kmlViewpoint, surface);
} else if (kmlNode.extent != null) {
// The node does not have a KML viewpoint. Build an ArcGIS Viewpoint based on the extent.
return await _createViewpointWithExtent(kmlNode.extent, surface);
} else {
return null;
}
}
// Function to create a Viewpoint based on a KML viewpoint.
Future<Viewpoint> _createViewpointWithKmlViewpoint(
KmlViewpoint kmlViewpoint,
Surface surface,
) async {
final Camera viewpointCamera;
if (kmlViewpoint.type == .lookAt) {
var lookAtPoint = kmlViewpoint.location;
if (kmlViewpoint.altitudeMode != .absolute) {
// If the elevation is relative, account for the surface's elevation.
final elevation = await surface.getElevation(kmlViewpoint.location);
final kmlViewpointAltitude = kmlViewpoint.location.z ?? 0;
lookAtPoint = ArcGISPoint(
x: kmlViewpoint.location.x,
y: kmlViewpoint.location.y,
z: kmlViewpointAltitude + elevation,
);
}
viewpointCamera = Camera.withLookAtPoint(
lookAtPoint: lookAtPoint,
distance: kmlViewpoint.range,
heading: kmlViewpoint.heading,
pitch: kmlViewpoint.pitch,
roll: kmlViewpoint.roll,
);
} else {
viewpointCamera = Camera.withLocation(
location: kmlViewpoint.location,
heading: kmlViewpoint.heading,
pitch: kmlViewpoint.pitch,
roll: kmlViewpoint.roll,
);
}
// Create Viewpoint using the viewpointCamera.
return Viewpoint.withLatLongScaleCamera(
latitude: 0,
longitude: 0,
scale: 1,
camera: viewpointCamera,
);
}
// Function to create a Viewpoint based on a KML node extent.
Future<Viewpoint?> _createViewpointWithExtent(
Envelope? extent,
Surface surface,
) async {
if (extent == null || extent.isEmpty) return null;
final extentCenter = extent.center;
final elevation = await surface.getElevation(extentCenter);
if (extent.width == 0 || extent.height == 0) {
// If the extent is not empty, but the width and height are still zero,
// default values (based on Google Earth) are used to create a camera.
final centerAltitude = extentCenter.z ?? 0;
final elevatedCenter = ArcGISPoint(
x: extentCenter.x,
y: extentCenter.y,
z: centerAltitude + elevation,
);
final viewpointCamera = Camera.withLookAtPoint(
lookAtPoint: elevatedCenter,
distance: 1000,
heading: 0,
pitch: 45,
roll: 0,
);
return Viewpoint.withLatLongScaleCamera(
latitude: 0,
longitude: 0,
scale: 1,
camera: viewpointCamera,
);
} else {
final extentBuilder = EnvelopeBuilder.fromEnvelope(extent)
..zMax += elevation
..zMin += elevation
..expandBy(1.1);
return Viewpoint.fromTargetExtent(extentBuilder.extent);
}
}
// Function to build list of ancestors for a given KML node. List includes the
// initial node.
List<KmlNode> _buildAncestorNodeList(KmlNode kmlNode) {
final ancestors = [kmlNode];
var parentNode = kmlNode.parentNode;
while (parentNode != null) {
ancestors.add(parentNode);
parentNode = parentNode.parentNode;
}
return ancestors;
}
}