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!

23 comments

Archived from the site's previous comment system. New comments are closed.

Pedro Delgado

Como calculo la longitud total de las rutas? me podrías orientar en eso por favor

dlee

Enumerate all routes and sum up the "Distance" of them to find the total length.

J.Theissen

Hello,
very fine documentation.
I am using gmap Version 1.7 and visual studio 2015 with c#.
I have a Database with polygons containing inner polygons which shall be transparent.
For example the region Brandenburg, excluding the entire region Berlin.

How can this be shown? I did not find an example.

srikanth ganji

Very well explained, so thankfull to find this blog. Nice work.

MANISH

Please let me know how to find the polygon click event in GMap.net wpf?

Tony

The event is in the GMap control. Look under Misc in the event list in your properties window

private void gMap_OnPolygonClick(GMapPolygon item, MouseEventArgs e){
}

Ardsman

Ok, sorry about the above.

I'd like to ask. I'm trying to make an app in Visual Studio that shows bus routes across Northern Ireland. The basic idea being that you'd select a button for a specific route, then it shows that route on a Google Maps Gmap plugin in red, while also showing the estimated position of the bus based on the time the users selected to view the route.

Is there any viable way of doing this? I've got some routes created thanks to help from this article, but they're just laid on top of the map. I've no idea how to make a marker move accurately based on time or other necessary factors.

Any help would be greatly appreciated. Thanks.

Kostas Soukaras (from Greece)

GMapOverlay routes = new GMapOverlay("routes");
List points = new List();
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); -----> This line must go right after
GMapOverlay routes = new GMapOverlay("routes"); or else the mapcontrol
needs refresh.
!!!!!EXCELLENT !!!!! Job

Yuyang

Hello
Thank everybody to develop wonderful GMap control. It's fantastic.
Now I have two problems. First is dragging by right button of mouse and route clicking.
Especially route does never response on mouse enter, leave and click.
Does anyone has experience to overcome this?
Thank you in advance.

Andrey O.

With this sequence of operators the polygon is NOT visible:

polygons.Polygons.Add(polygon);
gmap.Overlays.Add(polygons);

and with this:

gmap.Overlays.Add(polygons);
polygons.Polygons.Add(polygon);

the polygon IS visible....

Why?

Andrey O.

When I try to draw 2 routes, they always have the same color. Who knows how to make their colors different?
Is it at all possible?

faheem gul

how to get back longitude and latitude parameter when i changed the marker position. i want to save this new position of markar.

AlexV

Hello,

I have the multipolygon with hole:

MULTIPOLYGON (((106.8333000790002 58.136110001000134, 106.8333000790002 58.066669979000096, 107.13945260824767 57.963096203132523, 107.29094387700007 57.910647547000224, 107.42996661300009 57.92423511100003, 107.93810016900022 58.257568150000168, 107.93444398000014 58.353056004000166, 107.75000008200004 58.327779961000033, 107.19999998300017 58.333332980000193, 106.8333000790002 58.136110001000134), (107.51666894400012 58.166669490000061, 107.51666695100005 58.099998996000011, 107.21666501100026 58.033333996000067, 107.16666682300013 58.100000727000065, 107.51666894400012 58.166669490000061)))

How to add a multipolygon with hole to map use C# GMAP.NET?
I tried add the multipolygon with hole as polygon but I see the hole linked to the outside polygon by line.

MS SQL Server this polygon shows correctly.

paul

First of all, excellent work.

Is there a way to draw ellipsis, not only polygons?

Much appreciated,
Paul

paul

What I meant is, say draw a circle: a certain center in geo coordinates and then a radius, say 100NM.

Oscar

how can I put a label on the polygon?

Francesco

Hi, i would like to trace the gps route on the map, using as variables the incoming data in the serial port. For this reason i cannot have a list of points to add. Is there a way to use the pen, setting the initial position and then updating the position keeping the pen down? thank you in advance!

Kavya

Hi sir,
I tried below code, i return this code in button click event, after clicking button i am getting dark blue form,i am not getting any routes, please can anyone help me
GMapOverlay routes = new GMapOverlay("routes");
List points = new List();
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);

zikriya

hey i am facing a serious problem like show big red cross on background please help me if it is possible.Please follow this link for more info
https://stackoverflow.com/q...

Георгий Куприянов

Awesome library

Daniel Carrión

allows to search for places?

RVince

Is there a way to save the overlays / polygons and then load them later. I'm developing an application that creates a polygon for every suburb in Australia. The problem is that to insert the polygons from SQLCE databases with all the coordinates for the polygons it takes too long. If I can load them once and then save the map I can then load the map once with all the polygons loaded.

Silvia Aguilar

Hi, is there a way to click de map, and shows the latitude and longitude?