Skip to main content

KmlFileDataAdapter Class

A data adapter that loads data from KML and KMZ files, and displays it on vector layers.

Namespace: DevExpress.Xpf.Map

Assembly: DevExpress.Xpf.Map.v24.1.dll

NuGet Package: DevExpress.Wpf.Map

Declaration

public class KmlFileDataAdapter :
    MapGeoDataAdapter

Remarks

A .KML file uses the Keyhole Markup Language format to store geographic data such as locations, lines, polygons, images, text, etc. KML files have a tag-based structure with nested elements (similar to the XML standard). KML files can be distributed as KMZ files (zipped KML files with a .kmz extension).

The following table lists supported KML elements and corresponding map items:

KML element

Map item

Point

MapCustomElement

Polygon

MapPath

LinearRing

MapPolyline

LineString

MapPolyline

MultiGeometry

MapPath

Load KML/KMZ Data

The Map Control allows you to load data from KML/KMZ files and display it on a vector layer. You can also display vector data above an image tile map. For example, the image below shows a shape loaded from a KML file:

KMLFileData

Follow the steps below to load vector data from a KML/KMZ file:

  1. Create a KmlFileDataAdapter object.
  2. Specify the path to a KML/KMZ file via the MapGeoDataAdapter.FileUri property.
  3. Assign the KmlFileDataAdapter to the VectorLayer.Data property. Note that VectorLayer.Data is a content property. You can declare an adapter in XAML directly after a vector layer’s declaration without wrapping it in opening and closing VectorLayer.Data tags.
<dxm:VectorLayer>
    <dxm:KmlFileDataAdapter FileUri="kmlFile.kml" />
</dxm:VectorLayer>

Note: In the example above, the KML file’s Build Action is set to Resource.

Access and Customize Generated Items

Handle the MapGeoDataAdapter.ShapesLoaded event to access a collection of generated vector items (see the ShapesLoadedEventArgs.Shapes property) and customize their appearance.

<Window xmlns:dxm="http://schemas.devexpress.com/winfx/2008/xaml/map"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FileDataAdapters"
        x:Class="FileDataAdapters.MainWindow"
        xmlns:sys="clr-namespace:System;assembly=System"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <DataTemplate x:Key="template">
            <Image Source="{Binding}"></Image>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <dxm:MapControl x:Name="mapControl">
            <dxm:ImageLayer>
                <dxm:BingMapDataProvider BingKey="Your-BingKey-here"/>
            </dxm:ImageLayer>
            <dxm:VectorLayer x:Name="vectorLayer" DataLoaded="OnVectorLayerDataLoaded" ToolTipEnabled="True">
                <dxm:VectorLayer.Data>
                    <dxm:KmlFileDataAdapter ShapesLoaded="OnDataAdapterShapesLoaded">
                        <dxm:KmlFileDataAdapter.FileUri>
                            <sys:Uri>pack://application:,,,/FileDataAdapters;component/Data/subway-entrances.kmz</sys:Uri>
                        </dxm:KmlFileDataAdapter.FileUri>
                    </dxm:KmlFileDataAdapter>                    
                </dxm:VectorLayer.Data>
            </dxm:VectorLayer>
        </dxm:MapControl>
    </Grid>
</Window>
using DevExpress.Xpf.Map;
using System;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
namespace FileDataAdapters {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }
        private void OnVectorLayerDataLoaded(object sender, DataLoadedEventArgs e) {
            mapControl.ZoomToFitLayerItems(new LayerBase[] { vectorLayer });
        }
        private void OnDataAdapterShapesLoaded(object sender, ShapesLoadedEventArgs e) {

            foreach (MapItem item in e.Shapes) {
                if (item is MapCustomElement) {
                    ((MapCustomElement)item).ToolTipPattern = "{name}";
                    ((MapCustomElement)item).Content = new BitmapImage(new System.Uri(GetRelativePath("Images//circle.png")));
                    ((MapCustomElement)item).ContentTemplate = (DataTemplate)this.Resources["template"];
                }
            }
        }
        public static string GetRelativePath(string name) {

            DirectoryInfo dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
            while (dir != null) {
                string filePath = Path.Combine(dir.FullName, name);
                if (File.Exists(filePath))
                    return filePath;
                dir = Directory.GetParent(dir.FullName);
            }
            return string.Empty;
        }
    }
}

Note: In the example above, the KMZ file’s Build Action is set to Resource.

You can also group items of the same type. To do this, initialize the MapDataAdapterBase.Clusterer property with a clusterer.

See Also