Skip to content

Create KML Multi Track

View on GitHubSample viewer app

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

screenshot

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

Start the KML multi track sample application to begin moving along a simulated trail. Tap Record Track(s) to start recording your current path. 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 save your recorded tracks as a .kmz file to local storage. Then load the created .kmz file containing your KML multi-track to view all created tracks on the map. Tap the Recenter icon to position the location at the center of the map view.

How it works

  1. Create a Map with a basemap and a GraphicsOverlay to display the path geometry for your navigation route.
  2. Create a SimulatedLocationDataSource to drive the LocationDisplay.
  3. As you receive Location updates, add each point to a list of KmlTrackElement objects while recording.
  4. Once recording stops, create a KmlTrack using one or more KmlTrackElement objects.
  5. Combine one or more KmlTrack objects into a KmlMultiTrack.
  6. Save the KmlMultiTrack inside a KmlDocument, then export the document to a .kmz file.
  7. Load the saved .kmz file into a KmlDataset and locate the KmlDocument in the dataset's rootNodes. From the document's childNodes get the KmlPlacemark and retrieve the KmlMultiTrack geometry.
  8. Retrieve the geometry of each track in the KmlMultiTrack by iterating through the list of tracks and obtaining the respective KmlTrack.geometry.

Relevant API

  • KmlDataset
  • KmlDocument
  • KmlMultiTrack
  • KmlPlacemark
  • KmlTrack
  • LocationDisplay
  • SimulatedLocationDataSource

Tags

export, geoview-compose, hiking, kml, kmz, multi-track, record, track

Sample Code

CreateKmlMultiTrack.cppCreateKmlMultiTrack.cppCreateKmlMultiTrack.hCreateKmlMultiTrack.qml
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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 // [WriteFile Name=CreateKmlMultiTrack, Category=EditData]] // [Legal] // Copyright 2025 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. // [Legal]  #ifdef PCH_BUILD #include "pch.hpp" #endif // PCH_BUILD  // sample headers #include "CreateKmlMultiTrack.h"  // ArcGIS Maps SDK headers #include "Basemap.h" #include "Error.h" #include "ErrorException.h" #include "Geometry.h" #include "GeometryEngine.h" #include "Graphic.h" #include "GraphicListModel.h" #include "GraphicsOverlay.h" #include "GraphicsOverlayListModel.h" #include "KmlDataset.h" #include "KmlDocument.h" #include "KmlMultiTrack.h" #include "KmlNodeListModel.h" #include "KmlPlacemark.h" #include "KmlTrack.h" #include "KmlTrackElement.h" #include "Location.h" #include "LocationDisplay.h" #include "Map.h" #include "MapQuickView.h" #include "MapTypes.h" #include "MapViewTypes.h" #include "Point.h" #include "Polyline.h" #include "PolylineBuilder.h" #include "SimpleLineSymbol.h" #include "SimpleMarkerSymbol.h" #include "SimulatedLocationDataSource.h" #include "SimulationParameters.h" #include "SpatialReference.h" #include "SymbolTypes.h"  // Qt headers #include <QFile> #include <QFileInfo> #include <QFuture> #include <QStandardPaths> #include <QList> #include <QUrl>  using namespace Esri::ArcGISRuntime;  // helper method to get cross platform data path namespace {  QString defaultDataPath()  {  QString dataPath;  #ifdef Q_OS_IOS  dataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); #else  dataPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); #endif   return dataPath;  }   QString coastalTrailJson()  {  QFile jsonFile(":/Samples/EditData/CreateKmlMultiTrack/coastalTrail.json");  if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text))  {  qDebug() << "Failed to open JSON file";  return {};  }  return jsonFile.readAll();  } } // namespace   CreateKmlMultiTrack::CreateKmlMultiTrack(QObject* parent /* = nullptr */):  QObject(parent),  m_map(new Map(BasemapStyle::ArcGISStreets, this)),  m_graphicsOverlay(new GraphicsOverlay(this)),  m_locationSymbol(new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, Qt::red, 10, this)),  m_lineSymbol(new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, Qt::black, 3, this)) { }  CreateKmlMultiTrack::~CreateKmlMultiTrack() = default;  void CreateKmlMultiTrack::init() {  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");  qmlRegisterType<CreateKmlMultiTrack>("Esri.Samples", 1, 0, "CreateKmlMultiTrackSample"); }  MapQuickView* CreateKmlMultiTrack::mapView() const {  return m_mapView; }  // Set the view (created in QML) void CreateKmlMultiTrack::setMapView(MapQuickView* mapView) {  if (m_mapView)  {  m_mapView->setMap(nullptr); // Remove map from old mapView.  m_mapView->graphicsOverlays()->clear(); // Remove Graphics overlays from old mapView.  }   m_mapView = mapView;   if (!m_mapView)  {  return;  }   m_mapView->graphicsOverlays()->append(m_graphicsOverlay);  m_mapView->setMap(m_map);  emit mapViewChanged();   // Automatically enable recenter button when navigation pan is disabled  connect(m_mapView->locationDisplay(), &LocationDisplay::autoPanModeChanged, this, [this](LocationDisplayAutoPanMode autoPanMode)  {  m_isRecenterButtonEnabled = (autoPanMode == LocationDisplayAutoPanMode::Off);  emit isRecenterButtonEnabledChanged();  });   // Listen for changes in location  connect(m_mapView->locationDisplay(), &LocationDisplay::locationChanged, this, [this](const Location& location)  {  if (m_isRecordingTrack)  {  addTrackElement(location.position());  m_polylineBuilder->addPoint(location.position());  }  });   // turn on map view's navigation mode  m_mapView->locationDisplay()->setAutoPanMode(LocationDisplayAutoPanMode::Navigation); }  void CreateKmlMultiTrack::startNavigation() {  // Get the hiking path geometry  auto routeGeometry = geometry_cast<Polyline>(Geometry::fromJson(coastalTrailJson()));  constexpr double velocity = 25.0;  constexpr double horizontalAccuracy = 0.0;  constexpr double verticalAccuracy = 0.0;  constexpr double paddingInDips = 25.0;   // Create a simulated location data source from json data with simulation parameters to set a consistent velocity  auto* simulationParameters = new SimulationParameters(QDateTime::currentDateTime(), velocity, horizontalAccuracy, verticalAccuracy, this);  m_simulatedLocationDataSource = new SimulatedLocationDataSource(this);  m_simulatedLocationDataSource->setLocationsWithPolyline(routeGeometry, simulationParameters);   // Set the map's initial viewpoint  m_mapView->setViewpointGeometryAsync(routeGeometry, paddingInDips);   // Set the simulated location data source as the location data source for this app  m_mapView->locationDisplay()->setDataSource(m_simulatedLocationDataSource);   // Start the location data source  m_simulatedLocationDataSource->start(); }  void CreateKmlMultiTrack::addTrackElement(const Point& locationPoint) {  // Add a new element to the list of KML track elements  auto* trackElement = new KmlTrackElement(QDateTime::currentDateTime(), locationPoint, nullptr, this);  m_kmlTrackElements.append(trackElement);  emit elementsCountChanged();   // Add a graphic at the location's position  auto* graphic = new Graphic(locationPoint, m_locationSymbol, this);  m_graphicsOverlay->graphics()->append(graphic); }  void CreateKmlMultiTrack::startRecordingKmlTrack() {  m_isRecordingTrack = true;  emit isRecordingTrackChanged();   m_kmlTrackElements.clear();  emit elementsCountChanged();   m_polylineBuilder = new PolylineBuilder(SpatialReference::wgs84(), this); }  void CreateKmlMultiTrack::stopRecordingKmlTrack() {  if (m_kmlTrackElements.isEmpty())  {  return;  }   m_polylineBuilderList.append(m_polylineBuilder);   const KmlTrack kmlTrack(m_kmlTrackElements, KmlAltitudeMode::RelativeToGround);  m_kmlTracks.append(kmlTrack);  emit multiTrackCountChanged();   displayKmlTracks();  m_isRecordingTrack = false;  emit isRecordingTrackChanged(); }  void CreateKmlMultiTrack::displayKmlTracks() {  m_graphicsOverlay->graphics()->clear();  for (const auto* polyLineBuilder : m_polylineBuilderList)  {  // Add the polyline graphic to the map  auto* graphic = new Graphic(polyLineBuilder->toPolyline(), m_lineSymbol, this);  m_graphicsOverlay->graphics()->append(graphic);  } }  // Exports the [KmlMultiTrack] as a kmz file to local storage. void CreateKmlMultiTrack::exportKmlMultiTrack() {  // Create a KML document  auto* kmlDocument = new KmlDocument(this);   // Create a KML multi-track  const KmlMultiTrack multiTrack(m_kmlTracks, false);   // Add the multi-track as a placemark  auto* placemark = new KmlPlacemark(multiTrack, this);  kmlDocument->childNodesListModel()->insert(0, placemark);   // Define the save file path  const auto localKmlFilePath = defaultDataPath() + "/ArcGIS/Runtime/Data/kml/HikingTracks.kmz";  QFile localKmlFile(localKmlFilePath);  if (localKmlFile.exists())  {  localKmlFile.remove();  }  // Save KML file to local storage  // Write the KML document to the chosen path.  kmlDocument->saveAsAsync(localKmlFilePath).then(this, [localKmlFilePath, this]()  {  qDebug() << "Saved KmlMultiTrack: " + QFileInfo(localKmlFilePath).filePath();  stopNavigation();  }).onFailed([](const ErrorException&)  {  qDebug() << "Error saving KML file";  }); }  void CreateKmlMultiTrack::stopNavigation() {  if (m_simulatedLocationDataSource)  {  m_simulatedLocationDataSource->stop();  }  m_isShowTracksFromFileEnabled = true;  emit isShowTracksFromFileEnabledChanged(); }  void CreateKmlMultiTrack::loadLocalKmlFile() {  const auto localKmlFilePath = defaultDataPath() + "/ArcGIS/Runtime/Data/kml/HikingTracks.kmz";  const QFile localKmlFile(localKmlFilePath);  if (!localKmlFile.exists())  {  qDebug() << "Error locating KML file";  return;  }   auto* localKmlDataset = new KmlDataset(QUrl::fromLocalFile(localKmlFilePath), this);  connect(localKmlDataset, &KmlDataset::doneLoading, this, [this, localKmlDataset]()  {  if (localKmlDataset->loadStatus() != LoadStatus::Loaded)  {  qDebug() << "Error parsing KML file " + localKmlDataset->loadError().message();  return;  }   const auto* kmlDocument = dynamic_cast<KmlDocument*>(localKmlDataset->rootNodes().at(0));  if (!kmlDocument)  {  qDebug() << "Error: Root node is not a KmlDocument.";  return;  }   const auto* kmlPlacemark = dynamic_cast<KmlPlacemark*>(kmlDocument->childNodesListModel()->at(0));  if (!kmlPlacemark)  {  qDebug() << "Error: First child node is not a KmlPlacemark.";  return;  }   const auto kmlMultiTrack = static_cast<KmlMultiTrack>(kmlPlacemark->kmlGeometry());   QList<Geometry> geometries;  for (const auto& track : kmlMultiTrack.tracks())  {  geometries.append(track.geometry());  }  const auto allTracksGeometry = GeometryEngine::unionOf(geometries);  if (allTracksGeometry.isEmpty())  {  qDebug() << "KmlMultiTrack has no geometry";  return;  }   m_mapView->setViewpointGeometryAsync(allTracksGeometry, 25.0);   m_trackGeometries.append(allTracksGeometry);  for (const auto& track : kmlMultiTrack.tracks())  {  m_trackGeometries.append(track.geometry());  }  emit trackGeometriesCountChanged();  });   localKmlDataset->load(); }  void CreateKmlMultiTrack::previewKmlTrack(int index) {  m_mapView->setViewpointGeometryAsync(m_trackGeometries[index], 25.0); }  void CreateKmlMultiTrack::recenter() {  m_mapView->locationDisplay()->setAutoPanMode(LocationDisplayAutoPanMode::Navigation);  m_isRecenterButtonEnabled = false;  emit isRecenterButtonEnabledChanged(); }  void CreateKmlMultiTrack::reset() {  qDeleteAll(m_kmlTrackElements);  m_kmlTrackElements.clear();  m_kmlTracks.clear();  qDeleteAll(m_polylineBuilderList);  m_polylineBuilderList.clear();  m_trackGeometries.clear();  m_graphicsOverlay->graphics()->clear();  m_isShowTracksFromFileEnabled = false;  emit isShowTracksFromFileEnabledChanged(); }  int CreateKmlMultiTrack::elementsCount() const {  return m_kmlTrackElements.count(); }  int CreateKmlMultiTrack::multiTrackCount() const {  return m_kmlTracks.count(); }  int CreateKmlMultiTrack::trackGeometriesCount() const {  return m_trackGeometries.count(); }

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