GMAP.NET,为标记添加标签

时间:2016-11-03 09:43:59

标签: winforms google-maps label marker

我在C#(Winforms)中使用GMAPS,我想添加带标签的标记。我按照GMAP.NET adding labels underneath markers的回答,注意到实施存在问题。标记未绘制在正确的位置,标签全部绘制在彼此之上。我认为它没有正确地为标记调用OnRender方法?有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

遇到同样的问题,只是致电base.OnRender(g);并没有为我解决问题。诀窍是从GMarkerGoogle而不是GMapMarker派生,就像你提供的答案一样。

此外,我还必须对文本渲染进行一些调整。我提出了这个解决方案,对我来说很好用:

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
}