1

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.

enter image description here

rdoubleui
  • 3,554
  • 4
  • 30
  • 51
Mr.Smithyyy
  • 2,157
  • 12
  • 49
  • 95

2 Answers2

4

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)
rdoubleui
  • 3,554
  • 4
  • 30
  • 51
1

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.

Alexis Leclerc
  • 1,303
  • 1
  • 16
  • 26