windowsforms-devexpress-dot-xtramap-89c88082.md
A data adapter that loads data from GPX files, and displays it on vector layers.
Namespace : DevExpress.XtraMap
Assembly : DevExpress.XtraMap.v25.2.dll
NuGet Package : DevExpress.Win.Map
public class GpxFileDataAdapter :
FileDataAdapterBase,
IListSource,
IGpxOptionsProvider
Public Class GpxFileDataAdapter
Inherits FileDataAdapterBase
Implements IListSource,
IGpxOptionsProvider
Tip
To try out the GpxFileDataAdapter , see the GPX Data Adapter demo.
GPX files store coordinate-based data such as waypoints, routes, and tracks in an XML-like format.
The Map Control converts GPX file elements as follows:
<wpt> (waypoint) is displayed as MapDot.<rte> (route) is converted to a MapPolyline.<trk> (track) is represented by a MapPath.Routes and tracks are built based on trkpt coordinates nested inside <rte> or <trk> elements. If you enable the CreateRoutePoints property, map dots are generated for each trkpt element.
Follow the steps below to load data from a .GPX file:
private void Form1_Load(object sender, EventArgs e) {
ImageLayer imageLayer = new ImageLayer() {
DataProvider = new BingMapDataProvider {
BingKey = "Insert_your_BingKey_here",
Kind = BingMapKind.Road
}
};
mapControl1.Layers.Add(imageLayer);
VectorItemsLayer vectorLayer = new VectorItemsLayer();
mapControl1.Layers.Add(vectorLayer);
GpxFileDataAdapter dataAdapter = new GpxFileDataAdapter();
dataAdapter.FileUri = new Uri(GetRelativePath("boston-marathon-course.gpx"));
vectorLayer.Data = dataAdapter;
dataAdapter.CreateRoutePoints = true;
// Use the ItemsLoaded event to customize items the adapter generates.
dataAdapter.ItemsLoaded += OnDataAdapterItemsLoaded;
// You can call the MapControl.ZoomToFitLayerItems method in the DataLoaded event handler
// to zoom the map so that it displays all vector items.
vectorLayer.DataLoaded += OnVectorLayerDataLoaded;
}
private void OnDataAdapterItemsLoaded(object sender, ItemsLoadedEventArgs e) {
foreach (MapItem item in e.Items) {
if (item is MapPath) {
item.Stroke = Color.Black;
item.StrokeWidth = 2;
}
if (item is MapDot) {
item.Fill = Color.Orange;
item.Stroke = Color.Orange;
}
}
}
private void OnVectorLayerDataLoaded(object sender, DataLoadedEventArgs e) {
mapControl1.ZoomToFitLayerItems(new LayerBase[] { mapControl1.Layers[1] });
}
public static string GetRelativePath(string name) {
DirectoryInfo dir = new DirectoryInfo(Application.StartupPath);
while (dir != null) {
string filePath = Path.Combine(dir.FullName, name);
if (File.Exists(filePath))
return filePath;
dir = Directory.GetParent(dir.FullName);
}
return string.Empty;
}
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim imageLayer As ImageLayer = New ImageLayer() With {
.DataProvider = New BingMapDataProvider With {
.BingKey = "Insert_your_BingKey_here",
.Kind = BingMapKind.Road
}
}
mapControl1.Layers.Add(imageLayer)
Dim vectorLayer As VectorItemsLayer = New VectorItemsLayer()
mapControl1.Layers.Add(vectorLayer)
Dim dataAdapter As GpxFileDataAdapter = New GpxFileDataAdapter()
dataAdapter.FileUri = New Uri(GetRelativePath("boston-marathon-course.gpx"))
vectorLayer.Data = dataAdapter
dataAdapter.CreateRoutePoints = True
' Use the ItemsLoaded event to customize items the adapter generates.
dataAdapter.ItemsLoaded += AddressOf OnDataAdapterItemsLoaded
' You can call the MapControl.ZoomToFitLayerItems method in the DataLoaded event handler
' to zoom the map so that it displays all vector items.
vectorLayer.DataLoaded += AddressOf OnVectorLayerDataLoaded
End Sub
Private Sub OnDataAdapterItemsLoaded(ByVal sender As Object, ByVal e As ItemsLoadedEventArgs)
For Each item As MapItem In e.Items
If TypeOf item Is MapPath Then
item.Stroke = Color.Black
item.StrokeWidth = 2
End If
If TypeOf item Is MapDot Then
item.Fill = Color.Orange
item.Stroke = Color.Orange
End If
Next
End Sub
Private Sub OnVectorLayerDataLoaded(ByVal sender As Object, ByVal e As DataLoadedEventArgs)
mapControl1.ZoomToFitLayerItems(New LayerBase() {mapControl1.Layers(1)})
End Sub
Public Shared Function GetRelativePath(ByVal name As String) As String
Dim dir As DirectoryInfo = New DirectoryInfo(Application.StartupPath)
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
Object MapDisposableObject MapDataAdapterBase CoordinateSystemDataAdapterBase DevExpress.XtraMap.OuterDataAdapterBase FileDataAdapterBase GpxFileDataAdapter
See Also