1

I am using GMAPS in C# (Winforms) and I would like to add a marker with a label. I followed the answer at GMAP.NET adding labels underneath markers and noticed that there is an issue with the implementation. The markers are not plotted in the correct place and the labels are all plotted on top of each other. I think it is not correctly calling the OnRender method for the marker? Can anyone point me in the right direction?

Community
  • 1
  • 1
Tim Trewartha
  • 364
  • 3
  • 10

1 Answers1

1

In ran into the same issue and just calling base.OnRender(g); wasn't fixing it for me. The trick is to derive from GMarkerGoogle instead of GMapMarker as done in the answer you provided.

Also I had to do some tweaks with the text rendering. I came up with this solutions, works fine for me:

public class GmapMarkerWithLabel : GMarkerGoogle, ISerializable
{
    private readonly Font _font;
    private GMarkerGoogle _innerMarker;
    private readonly string _caption;

    public GmapMarkerWithLabel(PointLatLng p, string caption, GMarkerGoogleType type)
        : base(p, type)
    {
        _font = new Font("Arial", 11);
        _innerMarker = new GMarkerGoogle(p, type);

        _caption = caption;
    }

    public override void OnRender(Graphics g)
    {
        base.OnRender(g);

        var stringSize = g.MeasureString(_caption, _font);
        var localPoint = new PointF(LocalPosition.X - stringSize.Width / 2, LocalPosition.Y + stringSize.Height);
        g.DrawString(_caption, _font, Brushes.Black, localPoint);
    }

    public override void Dispose()
    {
        if (_innerMarker != null)
        {
            _innerMarker.Dispose();
            _innerMarker = null;
        }

        base.Dispose();
    }

    #region ISerializable Members

    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
        GetObjectData(info, context);
    }

    protected GmapMarkerWithLabel(SerializationInfo info, StreamingContext context)
        : base(info, context)
    { }

    #endregion
}