Extract route segments with TMS FNC Maps Summer Delphi project

Extract route segments with TMS FNC Maps Summer Delphi project

Germany has a wonderful network of bike routes throughout the whole country. Many of these bike routes are well thought out, along remarkable places, areas or elements in nature. The bike routes are in many cases clearly indicated with little road signs using a unique symbol specific for the route.Many of these classic & well-known routes are described in books or come with little booklets describing points of interest along the route and a website explaining it and offering GPX files for the routes for free download. It's almost a dream come true for bike lovers if it was not for the distance of these routes. Take the 100-Schl?sser Radweg?in the area of Münster Germany that consists of almost 1000km if biking routes.

Schloss Harkotten Sassenberg

The 100 Schl?sser Radweg is already divided in 4 routes, but even at almost 300km, it is a bit challenging to do this in one day. So, to prepare for a feasible one day bike tour, one would typically ride a part of this route, hence the need to extract a segment from the GPX route.

TMS FNC Maps polyline manipulation comes to help

In the latest version of TMS FNC Maps, we introduced several new helper functions that make it very easy to split or merge polylines or extract a polyline segment for a larger polyline. With it comes the handy feature to measure the distance of a polyline. In other words, we have all the tools in our hands to extract the desired distance for a bike route from an existing long bike route.?

The function we will use is:

NewPolyLine := TMSFNCMapsPolyLine.Segment(StartCoordinateIndex, EndCoordinateIndex);          

and the distance from any polyline, thus also the extracted segment, can be retrieved with PolyLine.Distance: single. This distance is measured in metres.

User interaction to extract the desired segment

We want to allow the user to visually indicate the segment desired for a bike tour and immediate give feedback on the selected distance. So, after loading a GPX file, we click on the map nearby a route polyline and we can use the function TMSFNCMaps.FindNearestPolylineCoordinate. With this function, we can check the nearest point on the route to the point where we clicked on the map. The first parameter is the coordinate where clicked and the second parameter tells TMS FNC Maps to search for the route point within a specified distance (in metres). When found, the function returns true and it returns the index of the polyline (in case the map has several polylines), the index of the point in the polyline array as well as its geo-coordinate.?

In this example, we use the following code to set the start coordinate of the desired segment and add a green marker to it:

var  
  pi,ci: integer;  
  cr: TTMSFNCMapsCoordinateRec;  
  mrk: TTMSFNCMapsMarker;  
  
begin  
  if TMSFNCMaps1.FindNearestPolylineCoordinate(AEventData.Coordinate.ToRec, 500, pi, ci, cr) then  
  begin  
    if TMSFNCMaps1.Markers.Count = 0 then  
    begin  
      startindex := ci;  
      mrk := TMSFNCMaps1.AddMarker(cr);  
      mrk.DefaultIcon.Enabled := true;  
      mrk.DefaultIcon.Fill := clGreen;  
      mrk.DefaultIcon.Size := 32;  
    end;  
  end;  
end;          

If there is already a marker, this means that the user clicked on the desired end point of the segment. So, in this case, we add the red marker for the endpoint.

if TMSFNCMaps1.Markers.Count = 1 then  
begin  
  endindex := ci;  
  mrk := TMSFNCMaps1.AddMarker(cr);  
  mrk.DefaultIcon.Enabled := true;  
  mrk.DefaultIcon.Fill := clred;  
  mrk.DefaultIcon.Size := 32;  
end;         

When the start and endpoint is known, we can extract the route segment. As the result of the segement creation is a new segment, we also want to visualize this on the map by setting this new segement polyline color in red and somewhat thicker to be clearly visible:

p := TMSFNCMaps1.Polylines[0].Segment(startindex,endindex);  
if Assigned(p) then  
begin  
  TMSFNCMaps1.Polylines.BeginUpdate;  
  p.StrokeColor := clRed;  
  p.StrokeWidth := 4;  
  p.StrokeOpacity := 1;  
  TMSFNCMaps1.Polylines.EndUpdate;  
  ShowDistance(p.Distance);  
end;          

In the last step, you can see we also update a label indicating the distance along the chosen segment.

Interactively changing the segment?

When there are already 2 markers on the map, we use a bit of a shortcut by deleting the last added marker and the last added polyline segment and calling the TMSFNCMaps1MapClick again, so it will perform the steps as-if only one marker was added:

if TMSFNCMaps1.Markers.Count = 2 then  
begin  
  TMSFNCMaps1.Polylines.Delete(1);  
  TMSFNCMaps1.Markers.Delete(1);  
  TMSFNCMaps1MapClick(Self, AEventData);  
end;          

Click on map versus click on polyline

When there is a polyline on the map, the user can either click on the map or click on the polyline. TMS FNC Maps generates two events for this, the OnMapClick event and the OnPolyElementClick event. As the event signature is fortunately the same, this makes it easy by assiging the same event handler to both events.? The result becomes:

Getting the result segment as GPX?

Also the last step is very easy. The segment polyline is the 2nd polyline on the map and the TMS FNC Maps component provides a direct method to export a polyline to GPX file with TMSFNCMaps.SaveToGPXFile()

In the app, this call is:

if SaveDialog1.Execute then  
begin  
  TMSFNCMaps1.SaveToGPXFile(TMSFNCMaps1.Polylines[1].Coordinates.ToArray, SaveDialog1.FileName);  
end;        

Get started

Recently we've?just released the newest version?of the unique & feature-rich?TMS FNC Maps?product. The?fully functional trial version?can be downloaded and you can discover its power with all of the included mapping services, including the fully free?OpenStreetMap?or?LeafletJS?maps! Grab the source code of the presented Delphi Summer project?here. The project uses the fully free OpenStreetMap mapping service, so you can enjoy this project without any risk on costs.?

要查看或添加评论,请登录

Bruno Fierens的更多文章

  • From VCL to WEB: Eat your own (dogfood) icecream

    From VCL to WEB: Eat your own (dogfood) icecream

    Regularly, we are contacted by Delphi developers asking for information how they can bring existing VCL desktop…

  • Visualize your routes with TMS FNC Maps Summer Delphi project

    Visualize your routes with TMS FNC Maps Summer Delphi project

    It's summer time, the ideal time for outdoor activitities. One of my personal favorite outdoor activitities is biking.

  • Jumpstart your path to create web apps with Delphi

    Jumpstart your path to create web apps with Delphi

    Today, you can create web client applications reusing all your skills of Object Pascal RAD component based development…

    1 条评论
  • In memory of Matthias Eissing

    In memory of Matthias Eissing

    Matthias at Klosterpforte, Harsewinkel Germany for TMS Training Days in 2018 It is a great shock and it is with deep…

    4 条评论
  • RAD Studio 12 and TMS components

    RAD Studio 12 and TMS components

    Celebrating four decades since the debut of Turbo Pascal, we now embrace Delphi 12, a fresh addition to RAD Studio 12…

    3 条评论
  • Debug TMS WEB Core web apps from the Delphi IDE

    Debug TMS WEB Core web apps from the Delphi IDE

    After months and months of research & development at tmssoftware.com , we're eager to share with you the first beta of…

    1 条评论
  • TMS VCL UI Pack v13.0 released

    TMS VCL UI Pack v13.0 released

    In the past couple of months, our team at tmssoftware.com did again a significant amount of work to bring a major new…

  • Showing protected PDF files in a web app

    Showing protected PDF files in a web app

    Imagine the following use-case: Someone wants to offer protected account-based access to specific resources, for…

  • Congrats Delphi 11!

    Congrats Delphi 11!

    With Embarcadero Technologies mentioning an upcoming RAD Studio 12 at https://www.embarcadero.

    1 条评论
  • Use Tailwind and the Tailwind workflow with TMS WEB Core

    Use Tailwind and the Tailwind workflow with TMS WEB Core

    Tailwind is a rapidly growing and popular open-source CSS framework. Different from Bootstrap and many other CSS…

社区洞察

其他会员也浏览了