Back to Devexpress

KmlFileDataAdapter Class

wpf-devexpress-dot-xpf-dot-map-8da6a962.md

latest9.3 KB
Original Source

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.v25.2.dll

NuGet Package : DevExpress.Wpf.Map

Declaration

csharp
public class KmlFileDataAdapter :
    MapGeoDataAdapter
vb
Public Class KmlFileDataAdapter
    Inherits 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:

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.
xaml
<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.

xaml
<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>
csharp
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;
        }
    }
}
vb
Imports DevExpress.Xpf.Map
Imports System
Imports System.IO
Imports System.Windows
Imports System.Windows.Media.Imaging

Namespace FileDataAdapters
    Public Partial Class MainWindow
        Inherits Window

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub OnVectorLayerDataLoaded(ByVal sender As Object, ByVal e As DataLoadedEventArgs)
            mapControl.ZoomToFitLayerItems(New LayerBase() {vectorLayer})
        End Sub

        Private Sub OnDataAdapterShapesLoaded(ByVal sender As Object, ByVal e As ShapesLoadedEventArgs)
            For Each item As MapItem In e.Shapes

                If TypeOf item Is MapCustomElement Then
                    CType(item, MapCustomElement).ToolTipPattern = "{name}"
                    CType(item, MapCustomElement).Content = New BitmapImage(New System.Uri(GetRelativePath("Images//circle.png")))
                    CType(item, MapCustomElement).ContentTemplate = CType(Me.Resources("template"), DataTemplate)
                End If
            Next
        End Sub

        Public Shared Function GetRelativePath(ByVal name As String) As String
            Dim dir As DirectoryInfo = New DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory)

            While dir IsNot Nothing
                Dim filePath As String = Path.Combine(dir.FullName, name)
                If File.Exists(filePath) Then Return filePath
                dir = Directory.GetParent(dir.FullName)
            End While

            Return String.Empty
        End Function
    End Class
End Namespace

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.

Inheritance

Object DispatcherObject DependencyObject Freezable MapDependencyObject MapDataAdapterBase MapGeoDataAdapter KmlFileDataAdapter

See Also

KmlFileDataAdapter Members

Load Vector Items from Vector Format Source

DevExpress.Xpf.Map Namespace