The GMap.NET control is a popular control for .NET that allows you to put interactive maps on your Windows Forms. In this third and last part of our GMap.NET tutorial, we show how you can add clickable polygons and routes to your maps, and how to style them.

Don’t forget to visit the first part of the tutorial for GMap.NET: Setting up your map first! Also, if you landed here first, be sure to have a look at part 2: GMap.NET: Adding markers to your map.

Adding polygons to the map

Markers may not always cut it. Sometimes you need to delimit an area of your map. For instance, you may need to indicate where new construction will take place, or who owns which land. Polygons allow you to show just that. To add a polygon to your map, you must define it as a list of latitude/longitude coordinates:

GMapOverlay polygons = new GMapOverlay("polygons");
List<PointLatLng> points = new List<PointLatLng>();
points.Add(new PointLatLng(48.866383, 2.323575));
points.Add(new PointLatLng(48.863868, 2.321554));
points.Add(new PointLatLng(48.861017, 2.330030));
points.Add(new PointLatLng(48.863727, 2.331918));
GMapPolygon polygon = new GMapPolygon(points, "Jardin des Tuileries");
polygons.Polygons.Add(polygon);
gmap.Overlays.Add(polygons);

The coordinates are placed in a generic list, which is then passed to the GMapPolygon constructor. I’ve created a special overlay just for the polygons, so that I can show or hide them all at the same time while my markers (which live in a different overlay) remain visible. You are free to put markers, polygons and routes all in the same overlay though.

Tip: don’t forget to add overlays to the map, or they (and the polygons in them) won’t show up.

The polygon that appears on the map will be white with a purple border (you may have to zoom in a little to see it). We can change these colors with the following code:

polygon.Fill = new SolidBrush(Color.FromArgb(50, Color.Red));
polygon.Stroke = new Pen(Color.Red, 1);

Here, I’ve given the polygon a red fill at at alpha = 50 (alpha ranges from 0, which is fully transparent, to 255, which is opaque), and red line around it with a thickness of one pixel.

Polygons in GMap.NET

Polygon events

While polygons cannot have tooltips, they can be clicked and you can intercept these clicks. The GMapPolygon class does not support any events, but you’ll find the OnPolygonClick event in the GMap.NET control itself (look for it in the Properties window, under Events). Thus you can set a Tag value for your polygon and read that in the event code, so you’ll know which polygon was clicked:

private void gmap_OnPolygonClick(GMapPolygon item, MouseEventArgs e)
{
    Console.WriteLine(String.Format("Polygon {0} with tag {1} was clicked",
        item.Name, item.Tag));
}

If you need even more interaction, the GMap.NET control also has the events OnPolygonEnter (for when the mouse cursor hovers over a polygon) and OnPolygonLeave (for when the mouse cursor leaves a polygon) available.

Adding routes to the map

In GMap.NET, routes are very similar to polygons, only without the fill color. The following code adds a route to the map:

GMapOverlay routes = new GMapOverlay("routes");
List<PointLatLng> points = new List<PointLatLng>();
points.Add(new PointLatLng(48.866383, 2.323575));
points.Add(new PointLatLng(48.863868, 2.321554));
points.Add(new PointLatLng(48.861017, 2.330030));
GMapRoute route = new GMapRoute(points, "A walk in the park");
route.Stroke = new Pen(Color.Red, 3);
routes.Routes.Add(route);
gmap.Overlays.Add(routes);

Note that I’ve set the route stroke color to red and given the stroke a thickness of 3, or it would blend in with the map since the default color is purple. You may have to zoom the map a little to see the route along the Jardin des Tuileries.

What’s special about routes is that you can have GMap.NET calculate the total route length using the route’s Distance property. This will return the route length in kilometers.

Conclusion

This tutorial should allow you to get started with GMap.NET. Here are some parting tips:

  • You can use different map providers. Some are faster than others. Also, some show more data than others. Depending on your location, this can make all the difference (Maputo, Mozambique is such as location – Yahoo Maps has almost no data).

  • When adding markers or polygons to an overlay, don’t forget to add the overlay to the map.

  • If your markers don’t show up, verify that you have MarkersEnabled set to True in the Properties panel for your GMapControl instance.

  • GMap.NET requires an internet connection to function properly. However, if no connection is available, you can still use cached information (if available) to show your maps. To do this, do:

GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.CacheOnly;

Thank you for reading!