ThinkGeo.com    |     Documentation    |     Premium Support

TrackOverlay - add vertex programatically

Hi all,
I’m using TrackOverlay to draw some custom shapes (lineas and polygons) and at certain cases I need to add next vertex not by clicking on map but programatically. Is something like this possible?
Thank you in advance

Regards
Martin

Hi Martin,

I think you can add the point like this:

map.TrackOverlay.TrackShapeLayer.InternalFeatures.Add(yourFeature)

Wish that’s helpful.

Regards,

Ethan

Hi Ethan,
thank you for your reply.
Let me explain my plan.
I need to create let’s say polygon by clicking on the map, so I start clicking point by point and then for example I need to insert next vertex of this polygon not by clicking on map but from code behind and then continue by clicking on map.
I have multiple markers on map in SimpleMarkerOverlay. While creating markers, I attach event handler to handle mouse clicks on markers like this

marker.MarkerMouseClick += Marker_MarkerMouseClick;

and later I have

    private void Marker_MarkerMouseClick(object sender, MouseButtonEventArgs e)
    {}

So I need to put something inside Marker_MarkerMouseClick function that will add next vertex to current shape (line or polygon) in map.TrackOverlay when I click on marker.

I hope this explains my task.
Thanks
Martin

Hi Martin,

Thanks for let us know your scenario, please try the solution as below:

  public class MyTrackOverlay : TrackInteractiveOverlay
{
    public MyTrackOverlay()
    {

    }

    public Collection<Vertex> Vertices
    {
        get { return base.Vertices; }
    }
}



        map.TrackOverlay = new MyTrackOverlay();
        map.TrackOverlay.TrackMode = TrackMode.Polygon;


    private void Marker_Click(object sender, EventArgs e)
    {
        Collection<Vertex> vs = (map.TrackOverlay.TrackShapeLayer.InternalFeatures[0].GetShape() as PolygonShape).OuterRing.Vertices;
        (map.TrackOverlay as MyTrackOverlay).Vertices.Insert(vs.Count - 1, new Vertex(6, 6));            
    }

The sample code insert a new vertex (6, 6).

You can modify it follow your requirement.

Regards,

Ethan

Hi Ethan,
thank you. This works fine while the marker is clicked not as first or last point of polygon creation.
But how to achieve the same if I click on marker as a first point - there is no feature exist at this moment in TrackShapeLayer.InternalFeatures.

Regards
Martin

Hi Martin,

I think click on marker cannot be set as the first point in this solution, because for the first point it should have some initialize logic.

Regards,

Ethan

Hi Ethan,
And is there any way how to properly start tracking from code behind?
I tried something like this and it looks that it’s doing what I expected

if (map.TrackOverlay.TrackMode == TrackMode.Line)
{
    if (map.TrackOverlay.IsEmpty)
    {
        InteractionArguments interactionArguments = new InteractionArguments();
        interactionArguments.WorldX = ((Marker)sender).Position.X;
        interactionArguments.WorldY = ((Marker)sender).Position.Y;
        interactionArguments.MouseButton = MapMouseButton.Left;

        map.TrackOverlay.ManipulationStarted(interactionArguments);
    }
    else
    {
        Collection<Vertex> vs = (map.TrackOverlay.TrackShapeLayer.InternalFeatures[0].GetShape() as LineShape).Vertices;
        (map.TrackOverlay as MyTrackOverlay).Vertices.Insert(vs.Count - 1, new Vertex(pointShape));
    }

Is this approach correct or it is working only by luck :smile:

Regards
Martin

Hi Martin,

I just found I hadn’t find the solution because I test it in winforms edition but not WPF, the API have a little different.

If you can make the track start by code, that means you can implement your requirement that set the first point by click marker.

Thanks for your share, which should be helpful if anyone else met the same problem.

Regards,

Ethan