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).

IMPORTANT: You are currently reading our old tutorial. We have a fresh GMap.NET tutorial about maps, markers, polygons and routes that’s updated for Visual Studio 2015 and GMap.NET 1.7!

View the updated tutorial

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 GetRoutemethod 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

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!

72 comments

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

Marty

How do I add intermediate points to the Route ?

Joao Bastos

Pois, eu estou exatamente com o mesmo problema

Joao Bastos

I have the same problem

Just me

Why dont you guys do this way:
set point 1 and point 2, where point 2 is the intermediate point.
Then set point 3.

P1 -> p2
Then
p2 -> p3

alex

No, that's just a grab from the full list of tweets. I don't tweet that much.

Rodrigo

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

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.

陈紫皖

hi,thank you ,i am a chinese boy, but i have some questions
i'm using something like this:
MapRoute route = GMap.NET.MapProviders.GoogleMapProvider.Instance.GetRouteBetweenPoints(points[0], points[1], false, false, 15);
but it return null, i'm a little lost, can you help me ,thank you!

GojiraSama

I had to use OpenStreet or something else. I don't think Google or Bing are allowing routes. I get nulls on both.

Rui

Ok, so no google or bing routes available anymore, however OpenStreetMapProvider gives routes also. Maybe they're not as optimized or accurate, but work fine on my app.

Franklin

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

Roy

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

Sergey

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"?

Sergey

How to set "arrows"?

dave

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

dave

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

alex

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

dave

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

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

dave

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

charl

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

what is the error if the GetRoute is the problem?

charl

need the answer asap ;(

kiran

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

Maul

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

Phạm Tuấn Anh

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

Ravan

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

Mark

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!

lu8890

hi Mark, what you mentioned is exactly what I am looking to do on my project.
If it's not too much trouble, do you mind provide some pointers on how to accomplish this?

thanks!

Achim

Hi,
is it possible anyway to use routing offline ?

Florian

Yes, you can preload the Mapdata of an specefic Area to your Cache. After that, you can use the Preload-Sector offline. Check radioman's demo out, there's an preload function. I use it too and it's works fine with my programm :D

mustafa

Hi,
Could you tell me that how I could get route coordinate array from route.
Thank you

Willian

Hi, I have a question!
So, I wanna create many routes and each route with your own color. someone how know I make it?

I tried to create each route with own overlay and after change the color of route, but when I change the color of one, all routes change too! :(
Same those that stay in diferent overlays!

Thanks!

Congratulations by project!

Afshin

Hi William,
did you find any solution for this problem? I have the same issue..

cheers,
Afshin

pendragon

I have the same worries, this is due to the method OnRender of GmapOverlays, it makes a foreach, so all routes are affecteds by the color defines by us, one can therefore not with this version of DDL make several routes with different colors.
At least I haven't found how to do, wait a future release I think

But it's a great work, so fun
Congrats

pendragon

I found the solution to have different colored routes for each of them, use it before adding a Overlays

r.Stroke = new Pen(GMapRoute.DefaultStroke.Brush);
r.Stroke.Width = WidthRoute;
r.Stroke.Color = Color.Orange;

If you use symply the demo code :

r.Stroke.Width = 2;
r.Stroke.Color = Color.SeaGreen;
All routes are the same Color ans Width, logical :)
Enjoy

Vladimir

here how you can resolve this problem
http://stackoverflow.com/qu...

code:
route.Stroke= (Pen)route.Stroke.Clone();
route.Stroke.Color = Color.Orange;

Jhon

Hello Alex, thanks for the tutorial ! I have few issue with the latest version and it seem the syntax doesn't match with yours, so can you provide us the same version used when you wrote this tutorial? I can hardly found any tutorial or documentation to help me out... I really need help for my school project.

Galbas

Hello!
Is it possible to read a static map in dwg or bmp format?
Thanks.

Eduardo

Hi great work!. Is possible insert a marker animated?; for example a pulsing marker? in NET

Arafii

Is there a way to add a route for live data? Meaning there's no end location, but the route updates with live GPS data?

Meindert

Hi alex, how can I check whether the mouse moves over a route...and then I want to show some information concerning that route...

dean

Hi

As of today we're experiencing adhoc failures when calling GMaps.Instance.GetRouteBetweenPoints. The call is returning null, The problem occurs in version 1 & 1.5.4.3 of the Core and WindowsForms.
We're using this overload PointLatLng start, PointLatLng end, bool avoidHighways, int Zoom, with these params -
50.966122,0.254626, 50.89757,0.07477, false, 0)

We know the lats and longs are good.

To reiterate, we are experiencing a vast level of failure when calling GetRouteBetweenPoints. We have been using this for years.

Many thanks

Ashi

We're getting the same problem when using the GetRouteBetweenPoints method.

Any solution or workaround would be welcomed!

Thanks!

Nelson

Please are there any alternatives to getting the accurate route between two points?

Nelson

when I instantiate route:
MapRoute route = GMap.NET.MapProviders.GoogleMapProvider.Instance.GetRoute(start, end, false, false, 15);
and call route.Distance, I always get an error, because route is null. Please is there any alternative. I really need this. Thanks

Ashi

Since the GetRouteBetweenPoints between point seems to return null, I've added the following code to do manually create an areal route (as the crew fly) if no route is returned:

route = GoogleMapProvider.Instance.GetRouteBetweenPoints(start, end, false, false, 15);
if (route == null)
{
List lstPoints = new List {start ,end};
route = new MapRoute (lstPoints,"Route" + i.ToString ());
}

BOIS

Hello,
I have the same problem , when calling GMaps.Instance.GetRoute The call is returning null,
We're using this overload (PointLatLng start, PointLatLng end, bool avoidHighways,bool walking mode, int Zoom)
with these params ( PointLatLngstart( 6,973888874054 47,87194442749 ),PointLatLng end( 6,973888874054 47,87194442749) , false , false , 0)
The lats and longs are good.

private double CalculDistance(PointLatLng start, PointLatLng end)
{
GMapProvider.WebProxy = WebRequest.GetSystemWebProxy();
GMapProvider.WebProxy.Credentials = CredentialCache.DefaultCredentials;
GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerOnly;
MapRoute route = GMap.NET.MapProviders.GoogleMapProvider.Instance.GetRoute(start, end, true, false, 0);

returns route.Distance
}

I have Gmap.NET.Core.dll et Gmap.NET.WindowsForm.dll at version 1.7.0.0

Since June 29, 2015, the function returns null, while it works for 1 or 2 years
Do you have a track or an index to help me.
Thank you in advance for your comments.

Meindert

I'm having the same issue...GetRoute gives a route that is NULL...so the application crashes constantly :(

Vinicius

GDirections ss;
var xx = GMapProviders.GoogleMap.GetDirections(out ss, start, end, false, false, false, false, false);
GMapRoute r = new GMapRoute(ss.Route, "My route");

try this....

EvoDriver

Same problem - ss ends up being null.

Meindert

Is it possible to select a whole street by using the GMapRoute object? Or is there another way to do that?

Javier

Well, If you use GoogleMapProvider, you'll get null in this:

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

But! I'm using another provider; OpenStreetMap, and it works perfectly. so it's like this:

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

Carlos

Thanks :D

Hari

Hi,
Thank you for this great library.
There is the possibility to calculate the travel time?

Thanks for your help.
Hari.

jalal

hi,
i have one problem in gmap . please i need to solve it .
i want to get directions duration in the gmap and save it in database for personal use .
how i can to do this ?

AHtesham Ur Rahim

how to display polygon or the route on the map.. even if it exits or not. please reply me as soon as possible.
here is the code.
GMapOverlay polyoverlay = new GMapOverlay("Polygons");
List points = new List();
points.Add(new PointLatLng(33.578989, 73.064408 ));
points.Add(new PointLatLng(33.57905, 73.064372));
points.Add(new PointLatLng(33.578855, 73.064433));
points.Add(new PointLatLng(33.578877, 73.064572));
points.Add(new PointLatLng(33.578413, 73.06473));
points.Add(new PointLatLng(33.578291, 73.064663));
GMapPolygon polygon = new GMapPolygon(points, "mypolygon");
polygon.Fill = new SolidBrush(Color.FromArgb(50, Color.LightGreen));
polygon.Stroke = new Pen(Color.Green, 1);
polyoverlay.Polygons.Add(polygon);
gMapControl1.Overlays.Add(polyoverlay);

when i run the code there is nothing to display... please help me out..
Thanks in advance

Sharon

Hi,

This is a great solution, love it.

I have two problems.
The route I am getting is not as optimized as the one I can see on Bing or Google, I was trying both map provider MapRoute route = GMap.NET.MapProviders.BingMapProvider.Instance.GetRoute(Start, End, false, false, 22); but not getting a good route.

Second issue, how do I zoom the map so the route will be in a full view without the need of the user to zoom in and out after the routing is completed.

Your help is greatly appreciated.

Sharon

Alvaro

MapRoute me retorna null, luego prosegui a cambiar de proveedor ahora ejecuta normal mi codigo pero no muestra la ruta

vamsi

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);

as i am running this , i has null value in route and i am getting error even if change the map provider to openstreetmapprovider.

please help me..needed it urgent. thanks in advance

Meysam

I create a rout:

PointLatLng start = new PointLatLng(34.633440, 50.867821);
PointLatLng end = new PointLatLng(34.618707, 50.844945);
MapRoute route = GMap.NET.MapProviders.GoogleMapProvider.Instance.GetRoute(
start, end, false, false, 15);

But Overlay word does not known, that I my route to it. did I miss a references?

Emilio J

Hi, How can I add point to polygon added to Overlays?

...
GMapOverlay polyOverlay = new GMapOverlay("polygons");
IList points = new List();
points.Add(new PointLatLng(-25.969562,32.585789));
points.Add(new PointLatLng(-25.966205,32.588171));
points.Add(new PointLatLng(-25.968134,32.591647));
points.Add(new PointLatLng(-25.971684,32.589759));
GMapPolygon polygon = new GMapPolygon(points, "mypolygon");
polygon.Fill = new SolidBrush(Color.FromArgb(50, Color.Red));
polygon.Stroke = new Pen(Color.Red, 1);
polyOverlay.Polygons.Add(polygon);
gmap.Overlays.Add(polyOverlay);
...
//When I press a button I want to add a point to "mypolygon" ¿how can I do?

Thank you.

Regards,
Emilio.

Iuri Braga

is it possible to animate a marker across the route? I've tried using a timer but I can't get it working... Does this API suport this? Or will I need to use something else( like LiveMaps from Microsoft?? ) HELP

valasidis vasileios

Is it possible to have bounded or multimap horizontally ?

Donovan

Hi, i have converted the coding in VB.net. So far everything has been working until i have tried to do the routing as described above.

here is my code:

1 Dim start As New PointLatLng(Unit_Latitude, Unit_Longitude)
2 Dim [end] As New PointLatLng(str_IncidentLat, str_IncidentLong)
3 Dim route As MapRoute = GMap.NET.MapProviders.GoogleMapProvider.Instance.GetRoute(start, [end], False, False, 15)

4 Dim AddedRoute As New GMapRoute(route.Points, "routes")
5 Dim routesOverlay As New GMapOverlay("routes")

6 routesOverlay.Routes.Add(AddedRoute)
7 LandingMap.Overlays.Add(routesOverlay)

There is an error on Line 4, it says :
Object reference not set to an instance of an object.' route was Nothing.

Donovan

I changed the Map Provider to BING, and now i get the routes.

Donovan

is there anyway to get the directions ?

Giuseppe

Hi, GetRoute using OSM provider for example "From Berlin to Milan", don´t work.

MapRoute route = ServerMap.GetRoute(start, end, false, false, (int)gmap.Zoom);
gmap.RoutesEnabled = true;
if (route != null)
{
GMapRoute r = new GMapRoute(route.Points, "My route");
GMapOverlay routesOverlay = new GMapOverlay("routes");
routesOverlay.Routes.Add(r);
r.Stroke = (Pen)r.Stroke.Clone();
r.Stroke.Color = Color.Orange;
gmap.Overlays.Add(routesOverlay);
if (route.Distance == 0.0)
{
//calculateRouteReverse(start, end);
label_dist.Text = "Dir Distance: 0.0 km";
MessageBox.Show("There is no route");
}
else
{
label_dist.Text = "Route Distance: " + Convert.ToString(route.Distance) + " km";
SetMarker(start.Lat, start.Lng, label_Start.Text);
SetMarker(end.Lat, end.Lng, label_End.Text);
loadMap(start.Lat, start.Lng);
}
}
else
{
MessageBox.Show("There is no route");
}

Why?

zikriya

hey i am facing a serious problem like show big red cross on background please help me if it is possible

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...

José María García Pérez

Hi,
I have one code with link with api fligth track in real time. The Gmap draw track in rela time as shape. Other sahe is a table info about flight. The code for create this case is very easy but my problem is:

A.- I need link track and info table with line.
B.- If you move the table info the line link move with table.

How can I create this link?

Saludos

Doomguy

Does it works with OpenStreetMapProvider?