Friday, June 8, 2018

Tutorial C# Rotate GMAP.NET Marker from Heading IMU or digital Compass

If you have a marker on Google Map which moves on a flight path/ road track but you want it to rotate when it turns. To rotate the image marker Icon from GMap.NET C#, in first create new class, add public class function named "GMapMarkerPlane"

using GMap.NET;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace GMap_offline
{
/// <summary>
/// used to override the drawing of the waypoint box bounding
/// </summary>
public class GMapMarkerPlane : GMapMarker
{
private readonly Bitmap icon = Resource.planetracker;
float heading = 0;
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);
try
{
g.RotateTransform(heading);
}
catch { }
g.DrawImageUnscaled(icon, icon.Width / -2, icon.Height / -2);
g.Transform = temp;
}
}
}
view raw common.cs hosted with ❤ by GitHub

For icon picture, create the icon and add it into resource folder.


              private readonly Bitmap icon = Resources.planetracker;

In the declaration, create new layer name from GMapOverlay:

             public static GMapOverlay routesoverlay;

Next, in main form create new layer and add it to the gmap:

            routesoverlay = new GMapOverlay("routes");
            gMap.Overlays.Add(routesoverlay);

and then, add the code below in the GPS heading function (use timer 1 second or more):

            routesoverlay.Markers.Clear();

            // Get the most up-to-date data received from the sensor.
            var curMeas = vn200.CurrentMeasurements;

            PointLatLng point = new PointLatLng((float)curMeas.LatitudeLongitudeAltitude.X,
                                                                           (float)curMeas.LatitudeLongitudeAltitude.Y);
            gMap.Position = point;

            var plane = new GMapMarkerPlane(point, (float)curMeas.YawPitchRoll.YawInDegs);
            routesoverlay.Markers.Add(plane);

and finally, the Plane Marker heading will following your heading data sensor.


Keyword: Rotate GMap.NET marker, C#, heading Marker, IMU, google map marker, GPS tracking heading marker.

6 comments:

  1. Excellent.. I was searching for this

    ReplyDelete
  2. Does anyone have any idea how to place the picture I added in the center of the coordinate I showed?

    ReplyDelete
  3. I can rotate the photo. There is no problem with that. but it was not centered in the coordinate I wanted. For example, the point I want is 0.0 but it always stands on it.

    ReplyDelete
  4. u never said what vn200 is what is it ?

    ReplyDelete
    Replies
    1. "vn200.CurrentMeasurements" is the heading data from IMU sensor V200 from Vectorav.

      Delete