Search code examples
tooltippolygongmap.net

How to add tooltip to Gmap.Net polygon?


I'm using Gmap.net great tools for enable mapping in my .net 4 desktop application. I'm adding polygons on form load event. But I can not set tooltip text for polygons.

any idea?


Solution

  • The only way to do it that I found is add a marker on the same overlay, and trigger its IsVisible property when the polygon is entered:

    var gm = new GMapControl();
    
    GMapOverlay polyOverlay = new GMapOverlay("polygons");
    
    List<PointLatLng> points = new List<PointLatLng>();
    points.Add(new PointLatLng(53.0, 10.0));
    points.Add(new PointLatLng(53.0, 11.0));
    points.Add(new PointLatLng(54.0, 11.0));
    points.Add(new PointLatLng(54.0, 10.0));
    
    var polygon = new GMapPolygon(points, "mypolygon");
    
    polygon.Fill = new SolidBrush(Color.FromArgb(50, Color.Red));
    polygon.Stroke = new Pen(Color.Red, 1);
    polygon.IsHitTestVisible = true;
    
    Controls.Add(gm);
    gm.Overlays.Add(polyOverlay);
    polyOverlay.Polygons.Add(polygon);
    
    polyOverlay.Markers.Add(new GMarkerCross(new PointLatLng(53.5, 10.5)) { ToolTipText = "Test me", IsVisible = false, ToolTipMode = MarkerTooltipMode.Always });
    
    gm.OnPolygonEnter += (poly) => polyOverlay.Markers.First().IsVisible = true;
    gm.OnPolygonLeave += (poly) => polyOverlay.Markers.First().IsVisible = false;
    

    Instead of GMarkerCross you could use a GMarkerGoogle with an empty Bitmap. Note: GMarkerGoogleType.none results in a System.ArgumentNullException when the mouse enters the polygon.