0

I'm using GMap.NET in C#.

My application looks like this..

enter image description here

getting gps logs from csv file, marking at the map, and showing speed(km/h) and heading(degree) at the right side.

I have an arrow marker png file. So, what I want to do is setting arrow marker at the center of the map and rotating it to point out its heading.

In javascript google map API, a marker icon has the rotation property. But I think the GMap.NET doesn't. I've googled all day long but I couldn't find it out.

Please, tell me how to do it..

Sangwoo Park
  • 125
  • 1
  • 1
  • 8

2 Answers2

0

This Source code is contained in mission planner (using GMap)

public class GMapMarkerPlane : GMapMarker
{
    const float rad2deg = (float)(180 / Math.PI);
    const float deg2rad = (float)(1.0 / rad2deg);

    private readonly Bitmap icon = Resources.HomePoint;

    float heading = 0;
    float cog = -1;
    float target = -1;
    float nav_bearing = -1;
    float radius = -1;

    public GMapMarkerPlane(PointLatLng p, float heading, float cog, float nav_bearing, float target, float radius)
        : base(p)
    {
        this.heading = heading;
        this.cog = cog;
        this.target = target;
        this.nav_bearing = nav_bearing;
        this.radius = radius;
        Size = icon.Size;
    }

    public GMapMarkerPlane(PointLatLng p, float heading)
        : base(p)
    {
        this.heading = heading;
        Size = icon.Size;
    }

    public override void OnRender(Graphics g)
    {
        Matrix temp = g.Transform;
        g.TranslateTransform(LocalPosition.X, LocalPosition.Y);

        g.RotateTransform(-Overlay.Control.Bearing);

        int length = 500;
        // anti NaN
        try
        {
            g.DrawLine(new Pen(Color.Orange, 2), 0.0f, 0.0f, (float)Math.Cos((heading - 90) * deg2rad) * length, (float)Math.Sin((heading - 90) * deg2rad) * length);
        }
        catch
        {
        }
        //g.DrawLine(new Pen(Color.Green, 2), 0.0f, 0.0f, (float)Math.Cos((nav_bearing - 90) * deg2rad) * length, (float)Math.Sin((nav_bearing - 90) * deg2rad) * length);
        //g.DrawLine(new Pen(Color.Black, 2), 0.0f, 0.0f, (float)Math.Cos((cog - 90) * deg2rad) * length, (float)Math.Sin((cog - 90) * deg2rad) * length);
        //g.DrawLine(new Pen(Color.Orange, 2), 0.0f, 0.0f, (float)Math.Cos((target - 90) * deg2rad) * length, (float)Math.Sin((target - 90) * deg2rad) * length);
        // anti NaN
        try
        {
            float desired_lead_dist = 100;

            double width =
                (Overlay.Control.MapProvider.Projection.GetDistance(Overlay.Control.FromLocalToLatLng(0, 0),
                    Overlay.Control.FromLocalToLatLng(Overlay.Control.Width, 0)) * 1000.0);
            double m2pixelwidth = Overlay.Control.Width / width;

            float alpha = ((desired_lead_dist * (float)m2pixelwidth) / radius) * rad2deg;

            if (radius < -1 && alpha > 1)
            {
                // fixme 

                float p1 = (float)Math.Cos((cog) * deg2rad) * radius + radius;

                float p2 = (float)Math.Sin((cog) * deg2rad) * radius + radius;

                g.DrawArc(new Pen(Color.HotPink, 2), p1, p2, Math.Abs(radius) * 2, Math.Abs(radius) * 2, cog, alpha);
            }

            else if (radius > 1 && alpha > 1)
            {
                // correct

                float p1 = (float)Math.Cos((cog - 180) * deg2rad) * radius + radius;

                float p2 = (float)Math.Sin((cog - 180) * deg2rad) * radius + radius;

                g.DrawArc(new Pen(Color.HotPink, 2), -p1, -p2, radius * 2, radius * 2, cog - 180, alpha);
            }
        }
        catch
        {
        }

        try
        {
            g.RotateTransform(heading);
        }
        catch
        {
        }
        g.DrawImageUnscaled(icon, icon.Width / -2, icon.Height / -2);

        g.Transform = temp;
    }
}

and then, add the code in your getGPS fuction (may be you used a thread or timer).

PointLatLng point = new PointLatLng(lat, lng);

var plane = new GMapMarkerPlane(point, heading);

objects.Markers.Add(plane);

AinMe
  • 1
0

Probably too late, but looking for a similar solution, I found that my approach was not the best one: Instead of trying to rotate the marker, you can rotate your png file, create a new marker using the same PointLatLng and update the marker.
Using the solution to this question: C#, rotating Graphics? for the RotateImage method.
And this is some silly example where I update all markers in an Overlay giving them a random direction.
Hope it helps!

if (gMapVisor.Overlays.Count > 0)
{
     for (int i = 0; i < gMapVisor.Overlays[0].Markers.Count; i++)
     {
          Bitmap iconoNavegacion;
          Random r = new Random();

          float angle = (float)(360 * r.NextDouble());
          iconoNavegacion = RotateImage(Properties.Resources.StatusUpdate_32x, angle);
          GMarkerGoogle chincheta = new GMarkerGoogle(gMapVisor.Overlays[0].Markers[i].Position, iconoNavegacion);

          gMapVisor.Overlays[0].Markers[i] = chincheta;
      }
}
jorgemc
  • 1
  • 1