Continuing from the previous GMap.NET Tutorial – Maps, markers and polygons on this site, this article shows how you can show a route on your map. It is assumed that you know how to setup a GMap.NET project, and set your map to your desired location (if not, read through the other tutorial first).
Like the maps themselves, routes are provider-specific. GMap.NET can use a whole slew of providers, but typically (for me at least) Google Maps seems to have the best data. When you want to show a route from point A to point B, the mapping provider will calculate the best route, using flags like “avoid highways” or “walking route”. Mapping providers with better data will provide better routes. When a mapping provider has little data on the area you’re showing, your route is likely going to be “as the crow flies”, since garbage in = garbage out.
Specifying the route
You can specify a route in two ways: by providing a starting and ending latitude/longitude pair, or by providing a string of text describing the start and end points. With a good mapping provider, the string will work. If the mapping provider has little knowledge of the area, you’re better off providing coordinate pairs, but it’s likely the route calculating will be bad as well.
So, in order to create a route, either do:
PointLatLng start = new PointLatLng(-25.974134,32.593042); PointLatLng end = new PointLatLng(-25.959048,32.592827); MapRoute route = GMap.NET.MapProviders.GoogleMapProvider.Instance.GetRoute( start, end, false, false, 15);
string start = "Avenida Armando Tivane, Maputo, Mozambique"; string end = "Rua 1301, Maputo, Mozambique"; MapRoute route = GMap.NET.MapProviders.GoogleMapProvider.Instance.GetRoute( start, end, false, false, 15);
You’ll notice that the map provider (Google, in this case) provides the GetRoute method that we need to create a MapRoute instance. You could use a different mapping provider (BingMapProvider, for instance) to create the route, and use it with yet another provider for the actual map images. After all, it all boils down to latitude/longitude coordinates.
Apart from the start and end points, the GetRoute method takes some more arguments. The two boolean flags are:
- avoidHighways – If set, the mapping provider will try to avoid highways, instead taking the scenic route (if supported);
- walkingMode – If set, the mapping provider will assume that you’re going on foot and include footpaths (if supported).
Finally, the last argument is the zoom mode. This is the zooming level at which the route will be calculated. Higher zoom modes yield better results. If you put in a low zoom mode, chances are your route will cut through buildings. A zoom mode of 15 has worked well for me.
Adding the route to the map
Now the MapRoute instance has been created, but it won’t show yet. We’ll need to do two more things: wrap the route up in a GMapRoute instance so that it can be named and shown, and then added it to an overlay. This neatly follows how everything else works in GMap.NET: everything goes into an overlay. So the first thing to check is something doesn’t show up is always, “Did I forget to add it to my overlay?”
GMapRoute r = new GMapRoute(route.Points, "My route");
The GMapRoute constructor takes a set of points. This means that although we had our mapping provider calculate the points for us, we could stick in a list of points ourselves, as well. Let’s add the GMapRoute instance to an overlay now, and add the overlay to our map:
GMapOverlay routesOverlay = new GMapOverlay("routes"); routesOverlay.Routes.Add(r); gmap.Overlays.Add(routesOverlay);
The result should be:
Styling the route
By default, a route is drawn using a fat, semitransparent blue line. This can be changed. The GMapRoute class provides a Stroke member that is an instance of Pen. Beware: do not create a new instance of Pen and assign it to the Stroke member, as this will create a memory leak. Instead, assign the Pen attributes directly like so:
r.Stroke.Width = 2; r.Stroke.Color = Color.SeaGreen;
Happy GMap.NET developing!