GMAP.NET adding labels underneath markers

笑着哭i 提交于 2019-12-23 02:25:05

问题


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.


回答1:


You need to create your own custom marker.

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
}

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)



回答2:


I'll answer here because this is the first question that pops up when looking to display a text marker for the WPF GMAP.NET library. Displaying a text marker with the WPF version of the library is actually much easier than in WinForms, or at least than the accepted answer.

The GMapMarker in WPF has a Shape property of type UIElement, which means you can provide a System.Windows.Controls.TextBlock object to display a text marker :

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

Since the marker displays the top left portion of the shape at the given position, you can play with the GMapMarker.Offset property to adjust the text position according to its dimensions. For instance, if you want the text to be horizontally centered on the marker's position :

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
});

The solution to get the TextBlock's dimensions was quickly taken from this question, so if you need a more accurate way of getting the block's dimensions to play with the offset I suggest you start from there.



来源:https://stackoverflow.com/questions/32057534/gmap-net-adding-labels-underneath-markers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!