Meyssam Toluie
Meyssam Toluie

Reputation: 1071

I can not Add route to GmapControl

My project is WPF. using these part of code I am trying to add route:

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

But in all articles they say I must add the created route to an overlay. And then add the overlays to my Control. But there is no overlay to add. How can I add the route to my Control?

Upvotes: 0

Views: 1572

Answers (1)

Franklin Chen - MSFT
Franklin Chen - MSFT

Reputation: 4923

But there is no overlay to add

In your code snippets, the MapRoute instance has been created. We'll need to do the following things:

1. Wrap the route up in a GMapRoute instance, the GMapRoute constructor takes a set of points.

2. Added GMapRoute instance to an overlay

3. Add overlay to GMapControl

Reference: ADDING THE ROUTE TO THE MAP

--------Update 5/11/2016--------

For WPF application, we have to wrap the route up in a GMapRoute instance and add into GMapControl.Markers:

 RoutingProvider rp = gmap1.MapProvider as RoutingProvider;
 if (rp == null)
 {
            rp = GMapProviders.OpenStreetMap; // use OpenStreetMap if provider does not implement routing
 }

 MapRoute route = rp.GetRoute(start, end, false, false, 15);

 if (route != null)
 {
            GMapRoute mRoute = new GMapRoute(route.Points);
            {
                mRoute.ZIndex = -1;
            }

            gmap1.Markers.Add(mRoute);

            gmap1.ZoomAndCenterMarkers(null);
 }
 else
 {
            System.Diagnostics.Debug.WriteLine("There is no route");
 }

I created a sample for you, please check here

Upvotes: 2

Related Questions