Create, save and preview a KML multi track, captured from a location data source.

Use case
When capturing location data for outdoor activities such as hiking or skiing, it can be useful to record and share your path. This sample demonstrates how you can collect individual KML tracks during a navigation session, then combine and export them as a KML multi track.
How to use the sample
Tap Record Track to start recording your current path on the simulated trail. Tap Stop Recording to end recording and capture a KML track. Repeat these steps to capture multiple KML tracks in a single session. Tap the Save button to convert the recorded tracks into a KML multi track and save it to a local .kmz file. Then use the picker to select a track from the saved KML multi track. Tap the Delete button to remove the local file and reset the sample.
How it works
- Create an
ArcGISMapwith a basemap and aGraphicsOverlayto display the path geometry for your navigation route. - Create a
SimulatedLocationDataSourceto drive theLocationDisplay. - As you receive
Locationupdates, add each point to a list ofKmlTrackElementobjects while recording. - Once recording stops, create a
KmlTrackusing one or moreKmlTrackElementobjects. - Combine one or more
KmlTrackobjects into aKmlMultiTrack. - Save the
KmlMultiTrackinside aKmlDocument, then export the document to a.kmzfile. - Load the saved
.kmzfile into aKmlDatasetand locate theKmlDocumentin the dataset’srootNodes. From the document’schildNodesget theKmlPlacemarkand retrieve theKmlMultiTrackgeometry. - Retrieve the geometry of each track in the
KmlMultiTrackby iterating through the list of tracks and obtaining the respectiveKmlTrack.geometry.
Relevant API
- KmlDataset
- KmlDocument
- KmlMultiTrack
- KmlPlacemark
- KmlTrack
- LocationDisplay
- SimulatedLocationDataSource
Tags
export, hiking, kml, kmz, multi track, record, track
Sample code
// 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:async';import 'dart:io';
import 'package:arcgis_maps/arcgis_maps.dart';import 'package:arcgis_maps_sdk_flutter_samples/common/common.dart';import 'package:material_ui/material_ui.dart';
class CreateKmlMultiTrack extends StatefulWidget { const CreateKmlMultiTrack({super.key});
@override State<CreateKmlMultiTrack> createState() => _CreateKmlMultiTrackState();}
class _CreateKmlMultiTrackState extends State<CreateKmlMultiTrack> with SampleStateSupport { // Create a controller for the map view. final _mapViewController = ArcGISMapView.createController(); // Create a location data source to simulate travel along a coastal trail. final _simulatedLocationDataSource = SimulatedLocationDataSource(); // Create overlays to display track elements and completed tracks. final _trackElementOverlay = GraphicsOverlay(); final _trackOverlay = GraphicsOverlay(); // Store the elements for the track currently being recorded. final _trackElements = <KmlTrackElement>[]; // Store the completed tracks that will form the KML multi-track. final _tracks = <KmlTrack>[];
// Manage subscriptions for recording locations and tracking auto-pan changes. StreamSubscription<ArcGISLocation>? _locationSubscription; StreamSubscription<LocationDisplayAutoPanMode>? _autoPanModeSubscription; // Store the temporary directory containing the generated KMZ file. Directory? _temporaryDirectory; // Store loaded track geometries, with all tracks combined at index zero. var _loadedTrackGeometries = <Geometry>[]; // Track the geometry currently selected for preview. var _selectedTrackIndex = 0; // Flags for controlling elements of the UI during different operations. var _ready = false; var _isRecording = false; var _isRecenterEnabled = false; var _isViewingSavedTracks = false;
@override void initState() { super.initState();
// Update the recenter button when the user pans away from navigation mode. _autoPanModeSubscription = _mapViewController .locationDisplay .onAutoPanModeChanged .listen((autoPanMode) { if (!mounted) return; setState(() { _isRecenterEnabled = autoPanMode != LocationDisplayAutoPanMode.navigation; }); }); }
@override void dispose() { // Stop the simulation and release subscriptions and temporary files. _simulatedLocationDataSource.stop().ignore(); _locationSubscription?.cancel().ignore(); _autoPanModeSubscription?.cancel().ignore(); _temporaryDirectory?.delete(recursive: true).ignore();
super.dispose(); }
@override Widget build(BuildContext context) { return Scaffold( body: SafeArea( top: false, left: false, right: false, child: Stack( children: [ Column( children: [ Expanded( // Add a map view to the widget tree and set a controller. child: Stack( children: [ ArcGISMapView( controllerProvider: () => _mapViewController, onMapViewReady: onMapViewReady, ), // Display the current recording or saved-track status. MapBanner(text: _statusText), ], ), ), // Display controls for recording or browsing saved tracks. Padding( padding: const EdgeInsets.all(8), child: _isViewingSavedTracks ? _buildTrackBrowser() : _buildRecordingControls(), ), ], ), // Display a progress indicator and prevent interaction until state is ready. LoadingIndicator(visible: !_ready), ], ), ), ); }
String get _statusText { // Describe the current recording or browsing state. if (_isViewingSavedTracks) { return "Saved KML multi-track to 'HikingTracks.kmz'."; } if (_isRecording) { return 'Recording KML track. Elements added: ${_trackElements.length}'; } return 'Tap record to capture KML track elements.'; }
Widget _buildRecordingControls() { // Build controls for recentering, recording, and saving the multi-track. return Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ IconButton.filledTonal( onPressed: _ready && _isRecenterEnabled ? _recenter : null, icon: const Icon(Icons.my_location), tooltip: 'Recenter', ), FilledButton( onPressed: _ready ? _toggleRecording : null, child: Text(_isRecording ? 'Stop Recording' : 'Record Track'), ), IconButton.filledTonal( onPressed: _ready && _tracks.isNotEmpty && !_isRecording ? _saveAndLoadKmlMultiTrack : null, icon: const Icon(Icons.save_alt), tooltip: 'Save KML multi-track', ), ], ); }
Widget _buildTrackBrowser() { // Build controls for previewing tracks from the saved KMZ file. return Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ DropdownButton( value: _selectedTrackIndex, items: List.generate( _loadedTrackGeometries.length, (index) => DropdownMenuItem( value: index, child: Text(index == 0 ? 'All Tracks' : 'KML Track #$index'), ), ), onChanged: (index) { if (index == null) return; unawaited(_previewTrack(index)); }, ), IconButton.filledTonal( onPressed: _reset, icon: const Icon(Icons.delete), tooltip: 'Delete KML multi-track', ), ], ); }
Future<void> onMapViewReady() async { // Create a map with a streets basemap style. final map = ArcGISMap.withBasemapStyle(.arcGISStreets); _mapViewController.arcGISMap = map;
// Configure renderers for track elements and completed tracks graphics overlay and then add to the controller. _trackElementOverlay.renderer = SimpleRenderer( symbol: SimpleMarkerSymbol(color: Colors.red, size: 10), ); _trackOverlay.renderer = SimpleRenderer( symbol: SimpleLineSymbol(color: Colors.black, width: 3), ); _mapViewController.graphicsOverlays.addAll([ _trackElementOverlay, _trackOverlay, ]);
// Create the simulated trail and frame it in the map view. final routePolyline = _createCoastalTrail(); await _mapViewController.setViewpointGeometry( routePolyline, paddingInDiPs: 25, );
// Start the simulated navigation and listen for recording locations. final navigationStarted = await _startNavigation(routePolyline);
// Enable the sample UI only when navigation starts successfully. if (mounted) setState(() => _ready = navigationStarted); }
Polyline _createCoastalTrail() { // Build a coastal trail near San Francisco in WGS 84 coordinates. final builder = PolylineBuilder(spatialReference: SpatialReference.wgs84); const coordinates = [ (-122.483531, 37.832119), (-122.483682, 37.831871), (-122.483806, 37.831658), (-122.483889, 37.831452), (-122.483979, 37.831180), (-122.484023, 37.830919), (-122.484028, 37.830776), (-122.484017, 37.830559), (-122.483985, 37.830468), (-122.483899, 37.830352), (-122.483761, 37.830252), (-122.483510, 37.830076), (-122.483343, 37.829934), (-122.483274, 37.829838), (-122.483255, 37.829737), (-122.483286, 37.829616), (-122.483368, 37.829521), (-122.483512, 37.829436), (-122.483632, 37.829409), (-122.483674, 37.829410), ];
// Add each coordinate to the route polyline. for (final coordinate in coordinates) { builder.addPoint( ArcGISPoint( x: coordinate.$1, y: coordinate.$2, spatialReference: SpatialReference.wgs84, ), ); } return builder.toGeometry() as Polyline; }
Future<bool> _startNavigation(Polyline routePolyline) async { // Generate one simulated location per second along the trail. _simulatedLocationDataSource.setLocationsWithPolyline( routePolyline, simulationParameters: SimulationParameters( startTime: DateTime.now(), speed: 25, ), );
// Connect the simulated source to the location display in navigation mode. final locationDisplay = _mapViewController.locationDisplay; locationDisplay.dataSource = _simulatedLocationDataSource; locationDisplay.autoPanMode = LocationDisplayAutoPanMode.navigation;
try { // Start the source and display before collecting location updates. await _simulatedLocationDataSource.start(); locationDisplay.start(); _locationSubscription ??= _simulatedLocationDataSource.onLocationChanged .listen(_recordLocation); return true; } on ArcGISException catch (exception) { if (mounted) { showExceptionDialog('Failed to start simulated navigation', exception); } return false; } }
void _recordLocation(ArcGISLocation location) { // Ignore location updates until recording is enabled. if (!_isRecording) return;
// Add a timestamped KML track element and display its position. setState(() { _trackElements.add( KmlTrackElement(when: DateTime.now(), coordinate: location.position), ); }); _trackElementOverlay.graphics.add(Graphic(geometry: location.position)); }
void _toggleRecording() { // Complete the active track or start a fresh track. if (_isRecording) { _completeTrack(); } else { _trackElements.clear(); _trackElementOverlay.graphics.clear(); setState(() => _isRecording = true); } }
void _completeTrack() { // Require at least two positions to create a KML track. if (_trackElements.length < 2) { showMessageDialog( 'Record at least two locations before stopping.', title: 'Not enough track elements', ); return; }
// Create a relative-to-ground KML track from the recorded elements. final track = KmlTrack( elements: List.of(_trackElements), altitudeMode: KmlAltitudeMode.relativeToGround, ); _tracks.add(track);
// Convert the track multipoint to a polyline for display. _trackOverlay.graphics.add(_graphicForTrack(track.geometry));
// Clear the active elements and return to navigation mode. _trackElements.clear(); _trackElementOverlay.graphics.clear(); setState(() => _isRecording = false); }
Future<void> _saveAndLoadKmlMultiTrack() async { // Disable controls while writing and reading the KMZ file. setState(() => _ready = false);
try { // Stop navigation before exporting the completed tracks. await _simulatedLocationDataSource.stop(); _mapViewController.locationDisplay.stop();
// Create a KML document containing one multi-track placemark. final multiTrack = KmlMultiTrack(tracks: _tracks); final document = KmlDocument(); document.childNodes.add(KmlPlacemark.withGeometry(multiTrack));
// Save the document to a temporary local KMZ file. _temporaryDirectory ??= await Directory.systemTemp.createTemp( 'create-kml-multi-track-', ); final kmzFile = File('${_temporaryDirectory!.path}/HikingTracks.kmz'); if (kmzFile.existsSync()) kmzFile.deleteSync(); await document.saveAs(kmzFileUri: kmzFile.uri);
// Load the KMZ and retrieve its KML multi-track geometry. final dataset = KmlDataset(kmzFile.uri); await dataset.load(); final loadedDocument = dataset.rootNodes.first as KmlDocument; final placemark = loadedDocument.childNodes.first as KmlPlacemark; final kmlGeometry = placemark.kmlGeometry; if (kmlGeometry is! KmlMultiTrack) { throw Exception('The saved placemark does not contain a multi-track.'); }
// Extract each track geometry and combine them for the "All Tracks" view. final trackGeometries = kmlGeometry.tracks .map((track) => track.geometry) .toList(); var allTracks = trackGeometries.first; for (final geometry in trackGeometries.skip(1)) { allTracks = GeometryEngine.union( geometry1: allTracks, geometry2: geometry, ); }
// Put the combined geometry first to match the track picker's ordering, // then switch from recording controls to the saved-track browser. if (!mounted) return; setState(() { _loadedTrackGeometries = [allTracks, ...trackGeometries]; _selectedTrackIndex = 0; _isViewingSavedTracks = true; _ready = true; }); _displayLoadedTracks(0); // Frame the combined geometry so every loaded track is initially visible. await _mapViewController.setViewpointGeometry( allTracks, paddingInDiPs: 25, ); } on Exception catch (exception) { // Restart navigation before restoring controls after a round-trip failure. if (!mounted) return; showExceptionDialog('Failed to save or load KML multi-track', exception); final navigationStarted = await _startNavigation(_createCoastalTrail()); if (mounted) setState(() => _ready = navigationStarted); } }
Future<void> _previewTrack(int index) async { // Display only the selected track, or every track for the first option. setState(() => _selectedTrackIndex = index); _displayLoadedTracks(index); await _mapViewController.setViewpointGeometry( _loadedTrackGeometries[index], paddingInDiPs: 25, ); }
void _displayLoadedTracks(int index) { // Index zero represents all tracks; subsequent indexes select one track. final geometries = index == 0 ? _loadedTrackGeometries.skip(1) : [_loadedTrackGeometries[index]]; // Replace the overlay contents so only the requested tracks are displayed. _trackOverlay.graphics ..clear() ..addAll(geometries.map(_graphicForTrack)); }
Graphic _graphicForTrack(Geometry geometry) { // Convert the KML track's multipoint geometry to a displayable polyline. final multipoint = geometry as Multipoint; final polylineBuilder = PolylineBuilder( spatialReference: multipoint.spatialReference, ); multipoint.points.forEach(polylineBuilder.addPoint); return Graphic(geometry: polylineBuilder.toGeometry()); }
void _recenter() { // Restore navigation auto-pan mode around the simulated location. _mapViewController.locationDisplay.autoPanMode = LocationDisplayAutoPanMode.navigation; }
Future<void> _reset() async { // Remove recorded and loaded tracks and delete the saved KMZ file. _trackElementOverlay.graphics.clear(); _trackOverlay.graphics.clear(); _trackElements.clear(); _tracks.clear(); _loadedTrackGeometries = []; _temporaryDirectory?.delete(recursive: true).ignore(); _temporaryDirectory = null;
// Restart the simulation from the beginning of the coastal trail. setState(() { _isViewingSavedTracks = false; _isRecording = false; _ready = false; }); final routePolyline = _createCoastalTrail(); await _mapViewController.setViewpointGeometry( routePolyline, paddingInDiPs: 25, ); final navigationStarted = await _startNavigation(routePolyline);
if (mounted) setState(() => _ready = navigationStarted); }}