GMAP.NET在标记

时间:2015-08-17 18:45:29

标签: winforms gmap.net

我刚开始使用gmap.net,我正在寻找在标记下添加标签的功能。我看到了那里的工具提示,但我希望在我的标记下有一个恒定的标签,并带有一个单词描述。

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

enter image description here

2 个答案:

答案 0 :(得分:2)

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

根据GMapMarker的来源和派生的GMarkerGoogle,我提出了这个简化的例子:

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
}

用法(假设GMap实例名为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)

答案 1 :(得分:0)

我将在此处回答,因为这是在寻找显示 WPF GMAP.NET库的文本标记时弹出的第一个问题。实际上,使用WPF版本的库显示文本标记要比WinForms容易得多,或者至少比接受的答案要容易。

WPF中的GMapMarker具有Shape类型的UIElement属性,这意味着您可以提供一个System.Windows.Controls.TextBlock对象来显示文本标记:

Markers.Add(new GMapMarker(new PointLatLng(latitude, longitude))
{
    Shape = new System.Windows.Controls.TextBlock(new System.Windows.Documents.Run("Label"))
});

由于标记在给定位置显示了形状的左上部分,因此您可以使用GMapMarker.Offset属性来根据其尺寸调整文本位置。例如,如果您希望文本在标记的位置水平居中:

var textBlock = new TextBlock(new Run("Label"));
textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
textBlock.Arrange(new Rect(textBlock.DesiredSize));
Markers.Add(new GMapMarker(new PointLatLng(request.Latitude, request.Longitude))
{
    Offset = new Point(-textBlock.ActualWidth / 2, 0),
    Shape = textBlock
});

this question迅速获得了TextBlock尺寸的解决方案,因此,如果您需要一种更准确的方法来获取带有偏移量的块尺寸,我建议您从这里开始

相关问题