"Highlight" Marker

Topics: Windows Forms
Jun 16, 2011 at 7:18 PM

Is there a way that I can highlight or maybe change the color of the marker on the go? I know I can probably create another marker with another color and add it to the map, but I was wondering if I could just highlight it or somehow make one of them stand out, knowing that in the future this will be changing, at one point one might be highlighted but after some criteria, another marker will be highlighted and this will be "done"
Thanks

Jun 16, 2011 at 7:30 PM

create custom marker

Jun 17, 2011 at 3:17 PM

I created a custom circle marker, you can easily set the diameter, color of outer pen, color of inner brush and some text with a custom font, to "highlight" a marker simply change these properties (for example in "OnMarkerEnter"). Here is the class:

public class GMapMarkerCircle : GMap.NET.WindowsForms.GMapMarker
    {
        #region Properties

        /// <summary>
        /// The pen for the outer circle
        /// </summary>
        public Pen OuterPen { get; set; }

        /// <summary>
        /// The brush for the inner circle
        /// </summary>
        public Brush InnerBrush { get; set; }

        /// <summary>
        /// The brush for the Text
        /// </summary>
        public Brush TextBrush { get; set; }

        /// <summary>
        /// The font for the text
        /// </summary>
        public Font TextFont { get; set; }

        /// <summary>
        /// The text to display inside of the marker 
        /// </summary>
        public String Text { get; set; }


        private int diameter = 10;
        /// <summary>
        /// The size of the circle
        /// </summary>
        public int CircleDiameter 
        {
            get 
            {
                return this.diameter;
            }
            set
            {
                diameter = value;
                this.Size = new System.Drawing.Size(diameter, diameter);
                Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2); 
            }
        }
        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="outer">The pen for the outer ring</param>
        /// <param name="inner">The brush for the inner circle.</param>
        /// <param name="diameter">The diameter in pixel of the whole circle</param>
        /// <param name="text">The text in the marker.</param>
        public GMapMarkerCircle(PointLatLng p, Pen outer, Brush inner, int diam, String text) : base(p)
        {
            OuterPen = outer;
            InnerBrush = inner;
            CircleDiameter = diam;
            this.Text = text;
            this.TextFont = new Font("Arial", (int)(diameter/2));
            this.TextBrush = Brushes.Black;
            Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2); 
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="p">The LatLongPoint of the marker.</param>
        /// <param name="outer">The pen for the outer ring</param>
        /// <param name="inner">The brush for the inner circle.</param>
        /// <param name="diameter">The diameter in pixel of the whole circle</param>
        /// <param name="textBrush">The brush for the text.</param>
        public GMapMarkerCircle(PointLatLng p, Pen outer, Brush inner, int diam, String text, Brush textBrush)
            : base(p)
        {
            OuterPen = outer;
            InnerBrush = inner;
            CircleDiameter = diam;
            this.Text = text;
            this.TextFont = new Font("Arial", (int)(diameter / 2));
            this.TextBrush = textBrush;
            Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2); 
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="p">The LatLongPoint of the marker.</param>
        /// <param name="outer">The pen for the outer ring</param>
        /// <param name="inner">The brush for the inner circle.</param>
        /// <param name="diameter">The diameter in pixel of the whole circle</param>
        /// <param name="textBrush">The brush for the text.</param>
        public GMapMarkerCircle(PointLatLng p, Pen outer, Brush inner, int diam, String text, Brush textBrush, Font textFont)
            : base(p)
        {
            OuterPen = outer;
            InnerBrush = inner;
            CircleDiameter = diam;
            this.Text = text;
            this.TextFont = textFont;
            this.TextBrush = textBrush;
            Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2); 
        }

        /// <summary>
        /// Render a circle
        /// </summary>
        /// <param name="g"></param>
        public override void OnRender(Graphics g)
        {
            g.FillEllipse(InnerBrush, new Rectangle(LocalPosition.X, LocalPosition.Y, diameter, diameter));
            g.DrawEllipse(OuterPen, new Rectangle(LocalPosition.X, LocalPosition.Y, diameter, diameter));

            if(!String.IsNullOrEmpty(this.Text))
            {
                SizeF sizeOfString = g.MeasureString(this.Text, this.TextFont);
                int x = (LocalPosition.X + diameter/2) - (int)(sizeOfString.Width / 2);
                int y = (LocalPosition.Y + diameter/2)- (int)(sizeOfString.Height / 2);
                g.DrawString(this.Text, this.TextFont, this.TextBrush, x, y);
            }
        }
    }

 

public class GMapMarkerCircle : GMap.NET.WindowsForms.GMapMarker
    {
        #region Properties

        /// <summary>
        /// The pen for the outer circle
        /// </summary>
        public Pen OuterPen { get; set; }

        /// <summary>
        /// The brush for the inner circle
        /// </summary>
        public Brush InnerBrush { get; set; }

        /// <summary>
        /// The brush for the Text
        /// </summary>
        public Brush TextBrush { get; set; }

        /// <summary>
        /// The font for the text
        /// </summary>
        public Font TextFont { get; set; }

        /// <summary>
        /// The text to display inside of the marker
        /// </summary>
        public String Text { get; set; }


        private int diameter = 10;
        /// <summary>
        /// The size of the circle
        /// </summary>
        public int CircleDiameter
        {
            get
            {
                return this.diameter;
            }
            set
            {
                diameter = value;
                this.Size = new System.Drawing.Size(diameter, diameter);
                Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2);
            }
        }


        #endregion


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="outer">The pen for the outer ring</param>
        /// <param name="inner">The brush for the inner circle.</param>
        /// <param name="diameter">The diameter in pixel of the whole circle</param>
        /// <param name="text">The text in the marker.</param>
        public GMapMarkerCircle(PointLatLng p, Pen outer, Brush inner, int diam, String text) : base(p)
        {
            OuterPen = outer;
            InnerBrush = inner;
            CircleDiameter = diam;
            this.Text = text;
            this.TextFont = new Font("Arial", (int)(diameter/2));
            this.TextBrush = Brushes.Black;
            Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="p">The LatLongPoint of the marker.</param>
        /// <param name="outer">The pen for the outer ring</param>
        /// <param name="inner">The brush for the inner circle.</param>
        /// <param name="diameter">The diameter in pixel of the whole circle</param>
        /// <param name="textBrush">The brush for the text.</param>
        public GMapMarkerCircle(PointLatLng p, Pen outer, Brush inner, int diam, String text, Brush textBrush)
            : base(p)
        {
            OuterPen = outer;
            InnerBrush = inner;
            CircleDiameter = diam;
            this.Text = text;
            this.TextFont = new Font("Arial", (int)(diameter / 2));
            this.TextBrush = textBrush;
            Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="p">The LatLongPoint of the marker.</param>
        /// <param name="outer">The pen for the outer ring</param>
        /// <param name="inner">The brush for the inner circle.</param>
        /// <param name="diameter">The diameter in pixel of the whole circle</param>
        /// <param name="textBrush">The brush for the text.</param>
        public GMapMarkerCircle(PointLatLng p, Pen outer, Brush inner, int diam, String text, Brush textBrush, Font textFont)
            : base(p)
        {
            OuterPen = outer;
            InnerBrush = inner;
            CircleDiameter = diam;
            this.Text = text;
            this.TextFont = textFont;
            this.TextBrush = textBrush;
            Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2);
        }

        /// <summary>
        /// Render a circle
        /// </summary>
        /// <param name="g"></param>
        public override void OnRender(Graphics g)
        {
            g.FillEllipse(InnerBrush, new Rectangle(LocalPosition.X, LocalPosition.Y, diameter, diameter));
            g.DrawEllipse(OuterPen, new Rectangle(LocalPosition.X, LocalPosition.Y, diameter, diameter));

            if(!String.IsNullOrEmpty(this.Text))
            {
                SizeF sizeOfString = g.MeasureString(this.Text, this.TextFont);
                int x = (LocalPosition.X + diameter/2) - (int)(sizeOfString.Width / 2);
                int y = (LocalPosition.Y + diameter/2)- (int)(sizeOfString.Height / 2);
                g.DrawString(this.Text, this.TextFont, this.TextBrush, x, y);
            }
        }

    }
Jun 17, 2011 at 3:39 PM

nice ;}

Jun 20, 2011 at 3:13 PM

Thank you for sharing the code, that seems very helpful and I will be able to modify it to fit my needs. Thanks!

Jun 23, 2012 at 5:18 AM

Hi Xandolph,

Can you post an example how to use your's class to highlight markers?

Can I associate it to other controls instead markers?

 

Thanks in advance.

Jun 24, 2012 at 1:49 PM

Hi, just set size/colors/font etc. in marker enter/leave events:

 

        /// <summary>
        /// Change the Size of the marker
        /// </summary>
        private void gMapControlDetail_OnMarkerEnter(GMapMarker m)
        {
            if (m is GMapMarkerCircle)
            {
                GMapMarkerCircle foo = (GMapMarkerCircle)m;
                foo.CircleDiameter = intActiveDiameter;
                ((SolidBrush)foo.InnerBrush).Color = Color.Red;
                foo.TextFont = fontBold;
                ((SolidBrush)foo.TextBrush).Color = Color.Black;
            }
        }

        /// <summary>
        /// Change the Size of the marker
        /// </summary>
        private void gMapControlDetail_OnMarkerLeave(GMapMarker m)
        {
            if (m is GMapMarkerCircle)
            {
                GMapMarkerCircle foo = (GMapMarkerCircle)m;
                foo.CircleDiameter = intPassiveDiameter;
                ((SolidBrush)foo.InnerBrush).Color = Color.White;
                foo.TextFont = fontNormal;
                ((SolidBrush)foo.TextBrush).Color = Color.Blue;
            }
        }