Blog
All Blog Posts | Next Post | Previous PostUsing GPX files with WayPoints (POI) in TMS FNC Maps for Delphi
Wednesday, October 16, 2024
What is a GPX file?
A GPX file is an XML-based format used to store GPS data. It can include waypoints (specific points of interest), tracks (a sequence of coordinates showing a path), and routes (a planned course of travel from one waypoint to another). These files are widely used in various applications, from hiking and cycling to geocaching and navigation.
An excerpt from the GPX file that shows track coordinates:
<trk> <name>Loire by bike - Loire à vélo</name> <trkseg> <trkpt lat="46.98812" lon="3.15795"> <ele>196</ele> </trkpt> <trkpt lat="46.98754" lon="3.15822"> <ele>197</ele> </trkpt> ... </trkseg> </trk>
Importing GPX files in TMS FNC Maps
TMS FNC Maps provides robust support for handling GPX files. Heres a step-by-step guide to importing GPX files with waypoints:
Adding a TTMSFNCMaps component to your form
First, place a TTMSFNCMaps component on your form. You can choose from various supported mapping services, all of which support GPX functionality. For this guide, well use msOpenLayers because it is free and doesnt require an API key.
procedure TForm1.FormCreate(Sender: TObject); begin TMSFNCMaps1.Service := msOpenLayers; end;
Loading and displaying an existing GPX file
In this guide we'll demonstrate how to display the contents of an existing GPX file on the map. There are numerous online resources for GPX files, we've chosen a bicycle route along the Loire river in France from RouteYou. Simply pass the filename as a parameter to the LoadFromGPXFile function, and the routes and waypoints found in the GPX file will automatically appear on the map.
TMSFNCMaps1.LoadGPXFromFile('filename.gpx');
Customizing GPX Waypoint (POI) display
By default, all waypoints in a GPX file are displayed with the same marker. However, our GPX file includes specific types for each waypoint. We can leverage this information to display unique markers for each waypoint type. For instance, we are particularly interested in highlighting the many castles found along the route. To achieve this, well use distinct colored markers and add labels with the castle names.
One of the Waypoints listed in the GPX file:
<wpt lat="47.47917" lon="1.18194"> <name>Château de Chaumont</name> <desc>The Château de Chaumont, officially Château de Chaumont-sur-Loire, is a castle in Chaumont-sur-Loire, Centre-Val de Loire, France. The castle was founded in the 10th century by Odo I, Count of Blois.</desc> <type>Castle</type> </wpt>
When loading data from the GPX file using the LoadFromGPXFile call, we set the second parameter to False to prevent automatic addition of polylines and markers. This allows us to manually add and customize these objects on the map.
Here are the steps to customize the display:
- Add Polyline
Use AddPolyline to draw the polyline based on the GPX track coordinates. - Add Markers
For each GPX waypoint (POI), use AddMarker . - Customize Markers
Set the fill color of the marker to blue for waypoints of type Castle. - Add Labels
Use AddLabel to display the castles name (included in the waypoint data) below the marker. - Adjust View
Use ZoomToBounds with the GPX route coordinates to ensure the polyline and markers fit within the map view.
var I: Integer; m: TTMSFNCMapsMarker; lb: TTMSFNCMapsLabel; p: TTMSFNCMapsPolyline; gpx: TTMSFNCMapsGPXRec; begin if OpenDialog1.Execute then begin gpx := TMSFNCMaps1.LoadGPXFromFile(OpenDialog1.FileName, False); TMSFNCMaps1.BeginUpdate; if (Length(gpx.Tracks) > 0) and (Length(gpx.Tracks[0].Segments) > 0) then begin p := TMSFNCMaps1.AddPolyline(gpx.Tracks[0].Segments[0]); p.StrokeColor := gcBlack; p.StrokeWidth := 3; end; for I := 0 to Length(gpx.Waypoints) - 1 do begin m := TMSFNCmaps1.AddMarker(gpx.WayPoints[I].WayPoint); m.DefaultIcon.Enabled := True; if gpx.WayPoints[I].WayPointType = 'Castle' then begin m.DefaultIcon.Fill := gcBlue; lb := TMSFNCMaps1.AddLabel(gpx.WayPoints[I].WayPoint, gpx.WayPoints[I].Name, gcBlack, gcWhite); end; end; TMSFNCMaps1.ZoomToBounds(gpx.Tracks[0].Segments[0]); TMSFNCMaps1.EndUpdate; end; end;
Conclusion
Importing GPX files with waypoints into TMS FNC Maps is a straightforward process that allows you to leverage the rich features of TMS Software's mapping components. Whether youre developing an application for outdoor activities, navigation, or any other GPS-related project, TMS FNC Maps provides the tools you need to handle GPX data efficiently.
Bart Holvoet
This blog post has not received any comments yet.
All Blog Posts | Next Post | Previous Post