本文介绍了GMAP.NET在标记下方添加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用gmap.net,我一直在寻找在标记下添加标签的功能.我看到有工具提示,但我想在标记下有一个带有一个单词描述的恒定标签.

I have just started using gmap.net and I was looking for the functionality of adding labels under the markers. I see there's tooltips but I would like to have a constant label under my marker with a one word description.

我搜索了文档或其他答案,但是找不到任何使我相信它没有实现的东西.如果有人可以验证这一点,我将不胜感激.

I searched for docs or other answers but I cannot find anything which leads me to believe that it is not implemented. If someone can verify this I would appreciate it.

推荐答案

您需要创建自己的自定义标记.

You need to create your own custom marker.

基于 GMapMarker的来源和派生的 GMarkerGoogle 我想到了这个简化的示例:

Based on the source of GMapMarker and the derived GMarkerGoogle I came up with this simplified example:

public class GmapMarkerWithLabel : GMapMarker, ISerializable
{
    private Font font;
    private GMarkerGoogle innerMarker;

    public string Caption;

    public GmapMarkerWithLabel(PointLatLng p, string caption, GMarkerGoogleType type)
        : base(p)
    {
        font = new Font("Arial", 14);
        innerMarker = new GMarkerGoogle(p, type);

        Caption = caption;
    }

    public override void OnRender(Graphics g)
    {
        if (innerMarker != null)
        {
            innerMarker.OnRender(g);    
        }

        g.DrawString(Caption, font, Brushes.Black, new PointF(0.0f, innerMarker.Size.Height));
    }

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

        base.Dispose();
    }

    #region ISerializable Members

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

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

    #endregion
}

用法(假设名为gmGMap实例):

Usage (assuming a GMap instance named gm):

GMapOverlay markerOverlay = new GMapOverlay("markers");
gm.Overlays.Add(markerOverlay);

var labelMarker = new GmapMarkerWithLabel(new PointLatLng(53.3, 9), "caption text", GMarkerGoogleType.blue);
markerOverlay.Markers.Add(labelMarker)

这篇关于GMAP.NET在标记下方添加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 00:42