5

I'm working on a programme with C# and I want to calculate the route, but it's returning null.

Here's my code ;

PointLatLng start = new PointLatLng(38.481858, 27.089006);
PointLatLng end = new PointLatLng(38.468447, 27.113793);

MapRoute route = GMap.NET.MapProviders.GoogleMapProvider
                                      .Instance.GetRoute(start, end, false, false, 15);
GMapRoute r = new GMapRoute(route.Points , "My route");
GMapOverlay routeOverlay = new GMapOverlay("route");
routeOverlay.Routes.Add(r);
gMap.Overlays.Add(routeOverlay);
double distance;
distance = route.Distance;

r.Stroke.Width = 2;
r.Stroke.Color = Color.OrangeRed;

I don't know where I am making mistakes. Any kind of help would be appreciated.

Old Fox
  • 8,629
  • 4
  • 34
  • 52
ilke kalkan
  • 101
  • 1
  • 8

5 Answers5

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

  • I was trying to get the distance from the route, I don't need directions but thanks for suggestion – ilke kalkan Jul 30 '15 at 11:24
  • @Vinicious Did this code work for you?On codeplex forums they told me that google has removed routing service. [link](http://greatmaps.codeplex.com/discussions/642101) – ilke kalkan Jul 30 '15 at 14:56
  • this give Object reference not set to an instance of an object.' on this line: GMapRoute r = new GMapRoute(ss.Route, "My route"); – Arie Aug 13 '20 at 13:51
  • This gives me the same error - ss ends up being null. – Matt Nov 05 '20 at 04:59
5

The problem is solved.. The reason why route returns null is because the routing service was been removed by google.

ilke kalkan
  • 101
  • 1
  • 8
1

Your Api key is invalid Add GMap from nuget use this code :

 public static double GetDistanceByRoute(double startLat, double startLng, double endLat, double endLng)
    {
        GoogleMapProvider.Instance.ApiKey = "Your Api Key";
        PointLatLng start = new PointLatLng(startLat, startLng);
        PointLatLng end = new PointLatLng(endLat, endLng);
        MapRoute route = GMap.NET.MapProviders.GoogleMapProvider.Instance.GetRoute(start, end, false, false, 15);
        return route.Distance;
    }
Omid Soleiman
  • 246
  • 3
  • 8
0
        PointLatLng startp = new PointLatLng(-25.974134, 32.593042);
        PointLatLng endp = new PointLatLng(-25.959048, 32.592827);
        MapRoute route = BingMapProvider.Instance.GetRoute(startp, endp, false, false, 15);
        GMapRoute r = new GMapRoute(route.Points,"Myroutes");
        GMapOverlay routesOverlay = new GMapOverlay("Myroutes");
        routesOverlay.Routes.Add(r);
        gmap.Overlays.Add(routesOverlay);
        r.Stroke.Width = 2;
        r.Stroke.Color = Color.SeaGreen; 

//use BingMapProvider

sgora
  • 1
0

Your API key is missing, i had the same problem, added the api key and it works fine.