GMap.NET Tutorial – Routes

GMap.NET LogoContinuing 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);

or

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:

Adding a route to GMap.NET

Adding a route to GMap.NET

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!

 

 


25 Comments

  1. Marty says:

    How do I add intermediate points to the Route ?

  2. cellurl says:

    Nice clean description. Side question: How current are tweets below? I want to add them to my site, but I don’t understand if they “cycle” thru old tweets. Yours seems to move a lot and I can’t believe you have that much going on at any given minute. Thanks for posting tutorial.

  3. Rodrigo says:

    Hi Alex, I can’t handle routes on my WPF app.
    There’s no GMapRoute class on my referenced dlls.

    I’m using something like this:
    MapRoute route = GMap.NET.MapProviders.GoogleMapProvider.Instance.GetRouteBetweenPoints(points[0], points[1], false, false, 15);

    but now I cannot add it to my MapControl.
    And there’s no overlays too.
    I’m a little bit lost here, can you please help me through this?
    Just wanna show the route between two points.

    • alex says:

      If there’s no GMapRoute class available, you must be using a different version of GMap.NET. Make sure you’re all updated, and then see if it’s not there. Use the class browser in Visual Studio.NET to find out there the class is – possible it has been renamed in newer versions, which is another possibility.

  4. Franklin says:

    Hey! The Overlay does not get displayed at all. When I debug, it stops just after the add.overlay

  5. Roy says:

    Hi Alex!
    Is it possible to make the routes draggble?

  6. Sergey says:

    Hi, Alex! How to learn the direction of movement on the route. For a example: if the car has drives in the both directions. How to set “arrays”?

  7. Sergey says:

    How to set “arrows”?

  8. dave says:

    like all my friends here i I can’t handle routes ????????????
    please give us answers !!!!!!!!!!

  9. dave says:

    like all my friends here i I can’t handle routes ????????????
    please give us answers !!!!!!!!!!

    • alex says:

      Hello Dave – can you be more specific? What is the problem?

      • dave says:

        hi alex thanks a lot for your response

        when i use this
        Dim route As MapRoute = GMap.NET.MapProviders.GoogleMapProvider.Instance.GetRouteBetweenPoints(start, endStop, False, False, 13)
        in route there is nothing
        i see in forums that radioman use getroute but it doesn t exist in the instance property

        a second questions how can i change the language of the google map now it is in english
        i need it in the local language

        • Toastbrot says:

          the method name got recently to “GetRouteBetweenPoints()”. please just look trough the class bevore you ask…

  10. dave says:

    hi alex thanks a lot for your response

    when i use this
    Dim route As MapRoute = GMap.NET.MapProviders.GoogleMapProvider.Instance.GetRouteBetweenPoints(start, endStop, False, False, 13)
    in route there is nothing
    i see in forums that radioman use getroute but it doesn t exist in the instance property

    a second questions how can i change the language of the google map now it is in english
    i need it in the local language

  11. charl says:

    MapRoute route = GMap.NET.MapProviders.GoogleMapProvider.Instance.GetRoute(start, end, false, false, 15);

    what is the error if the GetRoute is the problem?

  12. kiran says:

    i want to calculate the latitude and langetude while giving the address plese give me the code in c#

  13. Maul says:

    i currently working for my boat tracking. but when i use this maps i can’t make routes on sea. can you tell me how to make raw line on the maps based on latitude and longitude

  14. Phạm Tuấn Anh says:

    Hi, Alex! Can you help me about geocoding in Gmap.net? Please! Help me. I need you feedback soon

  15. Ravan says:

    Hi Alex,

    Very good tutorial.
    Can you tell me how to draw the polygon on the map and find x and y coordinate within this.

    Thanks

  16. Mark says:

    It would be good to mention in the article that you can define your own route by adding a series of PointLatLng. In other words, for just drawing any desired lines on the map!

  17. Achim says:

    Hi,
    is it possible anyway to use routing offline ?


Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">