I am using GMAPS in C# (Winforms) and I would like to add a marker with a label. I followed the answer at GMAP.NET adding labels underneath markers and noticed that there is an issue with the implementation. The markers are not plotted in the correct place and the labels are all plotted on top of each other. I think it is not correctly calling the OnRender method for the marker? Can anyone point me in the right direction?
Asked
Active
Viewed 1,746 times
1
-
Sounds like a bug that was fixed several years ago – geocodezip Nov 03 '16 at 12:38
-
Interesting, as far as I can tell I'm using the latest version (1.7.1). I managed to fix the marker placement issue by calling base.OnRender(g). – Tim Trewartha Nov 03 '16 at 13:19
1 Answers
1
In ran into the same issue and just calling base.OnRender(g);
wasn't fixing it for me. The trick is to derive from GMarkerGoogle
instead of GMapMarker
as done in the answer you provided.
Also I had to do some tweaks with the text rendering. I came up with this solutions, works fine for me:
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
}

DrSeuthberg
- 45
- 6