1

So what I am trying to do is showing a route on a winforms application using the GMap.NET nugget. But the problem I encountered is that it doesn't show the route using the roads. It's literately showing the shortest distances between 2 points, so to make it more accurate I did an extra API call to google maps Directions. So I have some extra points to make it more accurate but still between 2 points its just a straight line, not on roads.

route problem

Here you can see how I am loading the points.

public void ShowRoute(Coordinate[] coordinates)
    {
        if (coordinates.Length > 1)
        {
            List<PointLatLng> points = new List<PointLatLng>();

            foreach (var cor in coordinates)
            {
                Coordinate c = geo.ConvertCoordinates(cor);
                var corstring = geo.GetAdresByCor(c);
                var point = new PointLatLng(Convert.ToDouble(c.Latitude.Replace(".", ",")), Convert.ToDouble(c.Longitude.Replace(".", ",")));
                var marker = new GMarkerGoogle(point, GMarkerGoogleType.pink_pushpin);
                routeMarkers.Markers.Add(marker);
            }
            var routes = geo.DirectionsByCor(coordinates);

            foreach (var route in routes)
            {

                foreach (var step in route.Legs[0].Steps)
                {
                    var point = new PointLatLng(step.EndLocation.Latitude, step.EndLocation.Longitude);
                    points.Add(point);
                }
            }
            var r = new GMapRoute(points, "route");
            var routesOverlay = new GMapOverlay("routes");
            routesOverlay.Routes.Add(r);
            map.Overlays.Add(routeMarkers);
            map.Overlays.Add(routesOverlay);
        }
    }

Here is how my Map is loaded.

 private void map_Load(object sender, EventArgs e)
    {
        GMapProviders.GoogleMap.ApiKey = Properties.Settings.Default.ApiKey;


        geo = new GeocoderXY();
        map.MouseWheelZoomEnabled = true;
        map.DragButton = MouseButtons.Left;
        map.MapProvider = GMapProviders.GoogleMap;
        lng = 3.7074218;
        lat = 51.008157;

        GMapMarker marker = new GMarkerGoogle(new PointLatLng(lat, lng), GMarkerGoogleType.blue_pushpin);
        searchMarker = marker;

        markers.Markers.Add(marker);
        markers.Markers.Add(mouseMarker);

        map.Position = new NET.PointLatLng(lat, lng);
        map.MouseWheelZoomType = NET.MouseWheelZoomType.MousePositionWithoutCenter;
        map.MinZoom = 5;
        map.MaxZoom = 100;
        map.Zoom = 15;
        map.ShowCenter = false;
    }

I don't see what I'm doing wrong, if someone does see it, please let me know! Thanks

  • See following : https://stackoverflow.com/questions/41408821/flight-distance-using-google-api – jdweng Feb 05 '20 at 13:19
  • Thanks but this doesn't seem to be the same problem I have. I think the problem lies with the Nugget package I installed GMap.NET – Axel De Schuijmer Feb 05 '20 at 13:25
  • The link said : the distancematrix API supports travel_mode as a parameter - however your options are driving, walking, bicycling or transit so the API is useless. Are you using the Travel Mode? – jdweng Feb 05 '20 at 13:27
  • I don't use the Distancematrix API in for this. I use the Directions API, becuase the Distancematrix API doesn't give me points of the route and the direction API does. But yes I am using the Travel Mode as driving. – Axel De Schuijmer Feb 05 '20 at 13:33
  • Did you try the Geometry Library like suggested. – jdweng Feb 05 '20 at 14:07
  • Yes but this doesn't seem to help me in any way with this problem. – Axel De Schuijmer Feb 05 '20 at 14:09

1 Answers1

0

Still it's not clear why you're doing extra calls.Anyway, to get a route all it's needed a start and end locations and call GetDirections (apparently GoogleMapProvider is obsolete):

GDirections myDirections;
var route = GMapProviders.GoogleMap.GetDirections(out myDirections, start, end, false, false, false, false, false);

then use the directions to draw

var route = new GMapRoute(myDirections.Route, "route");
var routesOverlay = new GMapOverlay("routes");
routesOverlay.Routes.Add(route);

Unfortunately I dont have the VS to test but you got the idea.

Daniel B
  • 3,109
  • 2
  • 33
  • 42
  • The reason why I did extra api calls for the directions is because when I make a GMapRoute() I just get straight lines between 2 points. So with the directions call I added extra points to make the route more on roads. Tried your solution but I get a REQUEST_DENIED back when I do the GMapProviders.GoogleMap.GetDirections(), even though I added the API key. – Axel De Schuijmer Feb 05 '20 at 14:05