Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

GpxFileDataAdapter Class

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

Namespace: DevExpress.XtraMap

Assembly: DevExpress.XtraMap.v24.2.dll

NuGet Package: DevExpress.Win.Map

#Declaration

public class GpxFileDataAdapter :
    FileDataAdapterBase,
    IListSource,
    IGpxOptionsProvider

#Remarks

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.

image

Follow the steps below to load data from a .GPX file:

  1. Add a VectorItemsLayer object to the MapControl.Layers collection.
  2. Create a GpxFileDataAdapter object.
  3. Specify the path to a GPX file via the FileDataAdapterBase.FileUri property.
  4. Assign the GpxFileDataAdapter to the VectorItemsLayer.Data property.
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;
}

#Implements

#Inheritance

See Also