Heading for custom markers

Topics: Windows Forms
Feb 25, 2012 at 6:37 PM

Hey everyone :)

for my project , i successfully created custom markers using vb.net 2010 , here is the code if anyone wanna use it :

 

Public Class GMapMarker_Custom
    Inherits GMapMarker

    Dim _image As System.Drawing.Image
    Dim _point As PointLatLng

    Sub New(ByVal iPoint As PointLatLng)
        MyBase.New(iPoint)
        _point = iPoint
        ChangeImage()
    End Sub

    Public Overloads Overrides Sub OnRender(ByVal g As Graphics)
        MyBase.OnRender(g)
        g.DrawImageUnscaled(_image, System.Convert.ToInt32(LocalPosition.X), System.Convert.ToInt32(LocalPosition.Y))
    End Sub

    Public Sub ChangeImage()
        _image = My.Resources.ship
        MyBase.Size = New Drawing.Size(_image.Size.Width, _image.Size.Height)
        MyBase.Offset = New Drawing.Point(-(_image.Size.Width), -_image.Size.Height)
    End Sub
End Class

now i have a variable 'heading' goes from 0 to 359 , indicate the position relative to north

can anyone plz help me how to create this function :)

 

Feb 25, 2012 at 7:13 PM

use RenderTransform

Feb 25, 2012 at 7:26 PM

can you please explain how ?

Feb 26, 2012 at 11:47 AM

Please guys , can anyone help me ?

Mar 28, 2012 at 11:42 PM

Hi radioman

I have used this custome marker image for a few months now. I updated to the lastest beta version and now the Second marker (I only have two) is located top left of the map. On zoom the market seems to just float and not fixed to the LocalPosition.

It seem like the Marker has lost it local position information....???

thanks

Richard

Mar 29, 2012 at 9:03 AM

yes

Mar 29, 2012 at 9:09 AM

Is there a way to fix it..? 

Mar 29, 2012 at 9:24 AM

yes

Mar 29, 2012 at 9:33 AM

ok..  so it is in the DLL or something I need to fix in my code..?

:-)

Mar 29, 2012 at 9:40 AM

exactly, something ;}

Mar 29, 2012 at 9:44 AM

could you be a bit more informative please.

If you think the DLL is ok then say so. I am here to find an answer to the problem. I will help if I can but need more information.

Thanks

 

Mar 29, 2012 at 9:54 AM

there is no problem, you use experimental build, and you get experimental results

Mar 29, 2012 at 10:08 AM

But there is a problem as the marker does not show in the correct position, which it did before you updated the build ..???

I understand beta release etc, but if you say there is no problen then the code is of now use.

Can this be fixed in the DLL or not..?

Mar 29, 2012 at 10:15 AM

yes, it can ;} someday when someone, probably me, will have enough motivation

Mar 29, 2012 at 10:18 AM

thanks.. I hope soon as I can not make use of the code.

 It is a great product, just needs a tweak  ;-)

Mar 29, 2012 at 11:04 AM

'just' ;}

Mar 30, 2012 at 4:04 PM
Edited Mar 30, 2012 at 4:06 PM

OK, this is something i already make and this is the code for the custome marker with heading parameter

 

 

using System.Drawing;
using GMap.NET.WindowsForms;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System;


    namespace GMap.NET.WindowsForms.Markers
    {


        public class GMapMarkerImage : GMapMarker
        {
            public Pen Pen;
            float angle;
            float prevAngle;
            Bitmap image;

            public GMapMarkerImage(PointLatLng p,Bitmap pImage)
                : base(p)
            {
                Pen = new Pen(Brushes.AliceBlue, 2);

                // do not forget set Size of the marker
                // if so, you shall have no event on it ;}
                Size = new Size(55, 55);
                angle = 0;
                
                
                image = pImage;
               
                    
            }

            public override void OnRender(Graphics g)
            {
                Bitmap image1 = RotateImage(image,angle);
                g.DrawImage(image1, new Point(LocalPosition.X-image1.Width/2, LocalPosition.Y-image1.Height/2));   
            }

            public void setRotation(float Angle)
            {
                angle = Angle;
            }

            private static Bitmap RotateImage(Image image, float angle)
            {
                if (image == null)
                    throw new ArgumentNullException("image");

                const double pi2 = Math.PI / 2.0;

                // Why can't C# allow these to be const, or at least readonly
                double oldWidth = (double)image.Width;
                double oldHeight = (double)image.Height;

                // Convert degrees to radians
                double theta = ((double)angle) * Math.PI / 180.0;
                double locked_theta = theta;

                // Make sure theta is now [0, 2pi)
                while (locked_theta < 0.0)
                    locked_theta += 2 * Math.PI;

                double newWidth, newHeight;
                int nWidth, nHeight; // The newWidth/newHeight expressed as ints

                #region Explaination of the calculations
                /*
			 * The trig involved in calculating the new width and height
			 * is fairly simple; the hard part was remembering that when 
			 * PI/2 <= theta <= PI and 3PI/2 <= theta < 2PI the width and 
			 * height are switched.
			 * 
			 * When you rotate a rectangle, r, the bounding box surrounding r
			 * contains for right-triangles of empty space.  Each of the 
			 * triangles hypotenuse's are a known length, either the width or
			 * the height of r.  Because we know the length of the hypotenuse
			 * and we have a known angle of rotation, we can use the trig
			 * function identities to find the length of the other two sides.
			 * 
			 * sine = opposite/hypotenuse
			 * cosine = adjacent/hypotenuse
			 * 
			 * solving for the unknown we get
			 * 
			 * opposite = sine * hypotenuse
			 * adjacent = cosine * hypotenuse
			 * 
			 * Another interesting point about these triangles is that there
			 * are only two different triangles. The proof for which is easy
			 * to see, but its been too long since I've written a proof that
			 * I can't explain it well enough to want to publish it.  
			 * 
			 * Just trust me when I say the triangles formed by the lengths 
			 * width are always the same (for a given theta) and the same 
			 * goes for the height of r.
			 * 
			 * Rather than associate the opposite/adjacent sides with the
			 * width and height of the original bitmap, I'll associate them
			 * based on their position.
			 * 
			 * adjacent/oppositeTop will refer to the triangles making up the 
			 * upper right and lower left corners
			 * 
			 * adjacent/oppositeBottom will refer to the triangles making up 
			 * the upper left and lower right corners
			 * 
			 * The names are based on the right side corners, because thats 
			 * where I did my work on paper (the right side).
			 * 
			 * Now if you draw this out, you will see that the width of the 
			 * bounding box is calculated by adding together adjacentTop and 
			 * oppositeBottom while the height is calculate by adding 
			 * together adjacentBottom and oppositeTop.
			 */
                #endregion

                double adjacentTop, oppositeTop;
                double adjacentBottom, oppositeBottom;

                // We need to calculate the sides of the triangles based
                // on how much rotation is being done to the bitmap.
                //   Refer to the first paragraph in the explaination above for 
                //   reasons why.
                if ((locked_theta >= 0.0 && locked_theta < pi2) ||
                    (locked_theta >= Math.PI && locked_theta < (Math.PI + pi2)))
                {
                    adjacentTop = Math.Abs(Math.Cos(locked_theta)) * oldWidth;
                    oppositeTop = Math.Abs(Math.Sin(locked_theta)) * oldWidth;

                    adjacentBottom = Math.Abs(Math.Cos(locked_theta)) * oldHeight;
                    oppositeBottom = Math.Abs(Math.Sin(locked_theta)) * oldHeight;
                }
                else
                {
                    adjacentTop = Math.Abs(Math.Sin(locked_theta)) * oldHeight;
                    oppositeTop = Math.Abs(Math.Cos(locked_theta)) * oldHeight;

                    adjacentBottom = Math.Abs(Math.Sin(locked_theta)) * oldWidth;
                    oppositeBottom = Math.Abs(Math.Cos(locked_theta)) * oldWidth;
                }

                newWidth = adjacentTop + oppositeBottom;
                newHeight = adjacentBottom + oppositeTop;

                nWidth = (int)Math.Ceiling(newWidth);
                nHeight = (int)Math.Ceiling(newHeight);

                Bitmap rotatedBmp = new Bitmap(nWidth, nHeight);

                using (Graphics g = Graphics.FromImage(rotatedBmp))
                {
                    // This array will be used to pass in the three points that 
                    // make up the rotated image
                    Point[] points;
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;


                    /*
                     * The values of opposite/adjacentTop/Bottom are referring to 
                     * fixed locations instead of in relation to the
                     * rotating image so I need to change which values are used
                     * based on the how much the image is rotating.
                     * 
                     * For each point, one of the coordinates will always be 0, 
                     * nWidth, or nHeight.  This because the Bitmap we are drawing on
                     * is the bounding box for the rotated bitmap.  If both of the 
                     * corrdinates for any of the given points wasn't in the set above
                     * then the bitmap we are drawing on WOULDN'T be the bounding box
                     * as required.
                     */
                    if (locked_theta >= 0.0 && locked_theta < pi2)
                    {
                        points = new Point[] {new Point( (int) oppositeBottom, 0 ), new Point( nWidth, (int) oppositeTop ),new Point( 0, (int) adjacentBottom )};

                    }
                    else if (locked_theta >= pi2 && locked_theta < Math.PI)
                    {
                        points = new Point[] {new Point( nWidth, (int) oppositeTop ),new Point( (int) adjacentTop, nHeight ), new Point( (int) oppositeBottom, 0 )};
                    }
                    else if (locked_theta >= Math.PI && locked_theta < (Math.PI + pi2))
                    {
                        points = new Point[] { new Point( (int) adjacentTop, nHeight ), new Point( 0, (int) adjacentBottom ), new Point( nWidth, (int) oppositeTop )};
                    }
                    else
                    {
                        points = new Point[] { new Point( 0, (int) adjacentBottom ), new Point( (int) oppositeBottom, 0 ), new Point( (int) adjacentTop, nHeight )		
										 };
                    }

                    g.DrawImage(image, points);
                }

                return rotatedBmp;
            }
        }
    }

And you can use it this way

overlay = new GMapOverlay(gMapControl1, "overlay");
marker = new GMap.NET.WindowsForms.Markers.GMapMarkerImage(point,Properties.Resources.Marker_Image);
overlay.Markers.Add(marker);
gMapControl1.Overlays.Add(overlay);
marker.Position=new PointLatLng(lat, lon);
marker.setRotation((float)(theta)); //Theta in degrees
overlay.Markers[0]=marker;

Hope it help.

Logan

Mar 30, 2012 at 4:17 PM

public override void OnRender(Graphics g)
{
   using(Bitmap image1 = RotateImage(image,angle))
   {
      g.DrawImage(image1, new Point(LocalPosition.X-image1.Width/2, LocalPosition.Y-image1.Height/2)); 
   }
}

Apr 10, 2012 at 5:06 AM

Hi Logan

I tried your code with the new Beta update too. Still the same.. the second marker is just floating, not fixed to the map location.

Richard

Apr 10, 2012 at 5:13 AM

OK.. just started to work correctly now... not sure why.

R.

Jul 12, 2012 at 3:43 PM

Why don't use bitmap size??
public GMapMarkerImage(PointLatLng p, Bitmap pImage)
            : base(p)
        {
            Pen = new Pen(Brushes.AliceBlue, 2);

            // do not forget set Size of the marker
            // if so, you shall have no event on it ;}
            //Size = new Size(55, 55);
            Size = new Size(pImage.Width, pImage.Height);
            angle = 0;

            image = pImage;
        }
Dec 15, 2012 at 7:35 PM

muchas!!! gracias!! tu clase funciona perfectamente!!!!!!!

Feb 21, 2013 at 11:58 AM
Edited Feb 21, 2013 at 12:04 PM
Hi all,
first of all let me apologize for resurrecting an old topic.

I am very new to C# and I am trying to improve my knowledge by modifying and tinkering with the supplied demo's (Winforms), specifically I am customizing the "Flight radar demo" section, everything is working nicely, I am able to filter flights by airport departure/destination and also by airline.

I have created a custom marker for the flights and would like to rotate the marker to show the current heading for each flight.

I have added a new class to my project and added x_logan's code and radioman's modification to it.

When trying to use the class I have a bit of a problem

IntelliSense is happy with this part of my projects code and it compiles and works perfectly,
"marker = new GMap.NET.WindowsForms.Markers.GMapMarkerImage(d.point, Properties.Resources.FlightMarker);"

But, this part gives an IntelliSense error,
"marker.setRotation((float)(d.bearing)); //Theta in degrees"

'GMap.NET.WindowsForms.GMapMarker' does not contain a definition for 'setRotation' and no extension method 'setRotation' accepting a first argument type 'GMap.NET.WindowsForms.GMapMarker' could be found (are you missing a using directive or an assembly reference?)

Any clues as to what I'm doing wrong?
Like I said before I'm a no0b with C#, my previous experience being with VB.NET (beginner to intermediate), any help would be greatly appreciated.

P.s.
@ radioman, fantastic piece of work my friend, truly brilliant, thank you :-)

@ everyone else here, thank you so much for your contributions,,, I have learnt a lot by reading through the discussions section :-)

P.p.s.
Off topic I'm affraid, does anyone know how to copy IntelliSense errors to the clipboard, I've tried Googling it but can't find anything.
Feb 21, 2013 at 3:05 PM
well rotation isn't implemented by default, but you can use rotation function from this forum

the idea is to use a reference image and for each rotation update the markers image
Feb 21, 2013 at 3:18 PM
I too need rotated markers (bearing). I just uncommented the bearing code (I forget the filename). It looked promising, but didn't work out of the box.... I am curious why it was removed...
Feb 21, 2013 at 3:20 PM
Thanks for the fast response radioman.

Yes I understand that rotation isn't implemented by default, that's why I have added the code from Logans post (#17 on this page) and the code modifications from your post (#18 on this page) to a new class.

But IntelliSense still doesn't like the code.

"marker.setRotation((float)(d.bearing)); //Theta in degrees"

'GMap.NET.WindowsForms.GMapMarker' does not contain a definition for 'setRotation' and no extension method 'setRotation' accepting a first argument type 'GMap.NET.WindowsForms.GMapMarker' could be found (are you missing a using directive or an assembly reference?)

Sorry if I am being stupid, but I really am trying to learn.

Not easy for someone of my age (49) just starting out in programming ;-p
Feb 21, 2013 at 3:28 PM
use just use your custom marker instance, GMapMarker is base class
Jan 15, 2015 at 4:16 PM
p.s. marker rotation demo [left menu, Live->FlightRadar] and GMarkerArrow.cs
Marked as answer by radioman on 1/15/2015 at 8:16 AM