How to clear a Route

I display polylines 2 different ways on my TWebGMaps control. These lines are various road segments.
Method 1 uses the code below
var Pol : TPolylineItem;
ZIDX:=1;
Pol:=WebGMaps.Polylines.Add(...,ZIDX); // ZIndex
Result:=WebGMaps.CreateMapPolyLine(Pol.Polyline);

Method 2 is used to display Routes between 2 locations and uses the code below
var Route:TRoute;
Route:=WebGMaps.Directions[RouteIndex]; // Index to one of several "Directions" previously loaded
Route.Polyline.Color:=clRed;
Route.Polyline.ZIndex:=2;
WebGMaps.CreateMapPolyline(Route.Polyline);

This all works to paint Polylines on my map, but I want to be able to delete the "Routes" and not the original Method 1 Polylines. When I cycle through the WebGMaps.PolyLines list, the "Route" Polylines are not there. If I haven't added any Method 1 Polylines, and then Add a Method 2 Route.Polyline, then WebGMaps.Polylines.Count=0

So how to I get access to added Route Polylines so I can delete them, and what is the code to do so?
I can't use the WebGMaps.Routing.Clear method as it clears all my PolyLines. I only want to delete the ones created as "Routes"

I'm using version 3.2.0.1 on Delphi 10.4

Thanks for any help

Hi,

Please note that a CreateMapPolyline call will only create the polyline object on the map, but not add an item to the Polyline collection like in your Method 1.

You can remove a polyline object from the map by using DeleteMapPolyline with the polyline index as the parameter.

Bart,
Thanks for your response. I'm still a bit confused since the CreateMapPolyline routine is used in both my Method 1 and 2 examples.

Are you saying that the DeleteMapPolyline only works for my Method 1 code and not for clearing Route polylines? When I try to delete a route Polyline with DeleteMapePolyline, the TMS Code first checks the Polylines collection item count which is 0 if all I've done on the map is drawn one route, and I get an error.

So again, lets say I've created a route (using Method 2) with a ZIndex of 1, how do I remove that route from the map without deleting other Polylines (non-routes) I may have on the map?

I've tried the WebGMaps.Routing.Clear, but that seems to clear the routes AND all the Polylines collection of other lines I have drawn on the map. If that routine actually cleared just the "Routes", that would be perfect for my application.

Hi Eric,

To clarify, your method 2 won't result in items getting added to the Polylines collection. You could manually add the Route polyline to the Polylines collection like you are doing in method 2.

Then the DeleteMapPolyline should remove both the item from the Polylines collection and the polyline object from the map.

Bart,
I wrote a test program that I hope you can look at. It will draw 2 lines and 1route. I now add the route to the polylines collection and draw it that way but with a different ZIndex.

Now when I try to delete the lines (not the route), they get removed from the polylines colleciton and they are displayed on the map in a "not enabled" state. I can't get the route to disappear either. Can you look at the attached sample program and tell me what I'm missing here? You'll need to enter your APIKey in the APIKey variable.
TestMap2.zip (7.3 KB)

Thanks.

Hi Eric,

The polylines are actually added twice and removed only once.
Can you please make the following changes to your code to make this work as expected?

  • Remove these lines:
    WebGmaps1.CreateMapPolyline(pol.Polyline);

  • Change this line:
    WebGMaps1.DeleteMapPolyline(MapIDX);
    to
    WebGMaps1.DeleteMapPolyline(I);

Bart,
That worked. Thank you very much.
Eric

Happy to help!

Bart,
I can now draw and delete both Polylines and Routes, but this introduced a new issue for me. The tag strings show for the Polylines I draw, but not for the Route Polylines that I add to the Polylines collection, and once I add the Route Polylines, any regular Polyline I add after that does not show it's tag string.
Below are 2 ways I've tested drawing routes by adding the route polyline to the polylines collection. They are very similar and behave the same way in regards to the tag strings. When you hover over the "route" polyline, the mouse changes, but no tag string is displayed and the line doesn't change to the hover color.

procedure TMainForm.btnRouteClick(Sender: TObject);
var Route: TRoute;
WayPoints: TStringList;
begin
WebGMaps1.Directions.Clear;
WayPoints:=TStringlist.Create;
WebGMaps1.GetDirections(32.7766642,-96.7969879,31.7618778,-106.4850217, // dallas to elpaso
True, tmDriving, usImperial, lnEnglish, {AvoidHwys}False, {AllowTollroads}False, WayPoints, {OptimizeWayPoints}False);

if WebGMaps1.Directions.Count>0 then
begin
Route := WebGMaps1.Directions[0]; // initial route
Route.Polyline.Color:=clBlack;
Route.Polyline.HoverColor:=clBlue;
Route.Polyline.TagString:='Route 1';
WebGmaps1.Polylines.Add(True,False,False,nil,Route.Polyline.Path, clAqua, 100, 4, True,RouteIDX);
WebGMaps1.PolygonLabel.Visible:=True; // allows hover hints on routes
// Route.Free;
end;
if WayPoints<>nil then
FreeAndNil(Waypoints);
end;
-- Below is method using a TPolylineItem -------------------------------
procedure TMainForm.btnRouteClick(Sender: TObject);
var Route: TRoute;
Pol: TPolylineItem;
WayPoints: TStringList;
begin
WebGMaps1.Directions.Clear;
WayPoints:=TStringlist.Create;
WebGMaps1.GetDirections(32.7766642,-96.7969879,31.7618778,-106.4850217, // dallas to elpaso
True, tmDriving, usImperial, lnEnglish, {AvoidHwys}False, {AllowTollroads}False, WayPoints, {OptimizeWayPoints}False);

if WebGMaps1.Directions.Count>0 then
begin
Route := WebGMaps1.Directions[0]; // initial route
Pol:=WebGmaps1.Polylines.Add(True,False,False,nil,Route.Polyline.Path, clAqua, 100, 4, True,RouteIDX);
Pol.Polyline.Color:=clBlack;
Pol.Polyline.HoverColor:=clBlue;
Pol.Polyline.TagString:='Route 1';
WebGMaps1.PolygonLabel.Visible:=True; // allows hover hints on routes
// Route.Free;
end;
if WayPoints<>nil then
FreeAndNil(Waypoints);
end;

Thanks for any insight you have on this.
Eric

Hi Eric,

We are currently investigating this issue and will report back as soon as possible.

Bart,

Are you still looking into this issue?

Hi Eric,

Unfortunately, due to other ongoing projects, we had to delay investigation of this issue.

  1. There is an issue with the Polyline ItemIndex if used in combination with GetDirections.
    Please make sure to manually set the ItemIndex to a correct value like this:
    Pol.Polyline.ItemIndex := WebGMaps1.Polylines.Count;
    We'll have to investigate if this issue can be resolved in a future version of TMS VCL WebGMaps.

  2. In the first method you are assigning the settings to the route. But only the path of the route is used when adding a polyline to the map with Poylines.Add. This means the Route settings are ignored. So you should assign the settings to the resulting polyline like you did in the second method.