View in MAUI WPF WinUI View on GitHub

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

Create KML multi-track

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 Start Navigation to begin moving along a simulated trail. Tap Record Track 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.

How it works

  1. Create an ArcGISMap 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.xaml CreateKmlMultiTrack.xaml CreateKmlMultiTrack.xaml.cs
<UserControl x:Class="ArcGIS.WinUI.Samples.CreateKmlMultiTrack.CreateKmlMultiTrack"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:esri="using:Esri.ArcGISRuntime"
xmlns:esriUI="using:Esri.ArcGISRuntime.UI.Controls">
<UserControl.Resources>
<Style x:Key="IconStyle" TargetType="Button">
<Style.Setters>
<Setter Property="FontFamily" Value="{StaticResource CalciteUIIconsMediumFontFamily}" />
<Setter Property="FontSize" Value="24" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="Margin" Value="5" />
<Setter Property="CornerRadius" Value="5" />
</Style.Setters>
</Style>
</UserControl.Resources>
<Grid>
<esriUI:MapView x:Name="MyMapView" />
<Border Style="{StaticResource BorderStyle}">
<StackPanel>
<StackPanel x:Name="RecordingUI">
<TextBlock x:Name="ElementsCountTextBox" Text="Click record to capture KML track elements." />
<TextBlock x:Name="TracksCountTextBlock" Text="Number of tracks in MultiTrack: 0" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button x:Name="RecenterButton"
Click="RecenterButton_Click"
Content="{StaticResource CalciteUIIcons_Glyph_GpsOn}"
IsEnabled="False"
Style="{StaticResource IconStyle}"
ToolTipService.ToolTip="Recenter" />
<Button x:Name="RecordTrackButton"
Grid.Column="1"
Margin="5"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Click="RecordTrackButton_Click"
Content="Record Track" />
<Button x:Name="SaveButton"
Grid.Column="2"
Click="SaveButton_Click"
Content="{StaticResource CalciteUIIcons_Glyph_Save}"
IsEnabled="False"
Style="{StaticResource IconStyle}"
ToolTipService.ToolTip="Save" />
</Grid>
</StackPanel>
<StackPanel x:Name="ViewingUI" Visibility="Collapsed">
<TextBlock Text="Displaying contents of saved HikingTracks.kmz file." />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ComboBox x:Name="TracksComboBox"
Margin="5"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
DisplayMemberPath="Key"
SelectionChanged="TracksComboBox_SelectionChanged" />
<Button Grid.Column="1"
Click="ResetButton_Click"
Content="{StaticResource CalciteUIIcons_Glyph_Reset}"
Style="{StaticResource IconStyle}"
ToolTipService.ToolTip="Reset" />
</Grid>
</StackPanel>
</StackPanel>
</Border>
</Grid>
</UserControl>