Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GMap.NET route is returning null

Tags:

c#

gmap.net

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.

like image 639
ilke kalkan Avatar asked Jul 29 '15 06:07

ilke kalkan


3 Answers

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

like image 98
Vinicius Lobo Silva Avatar answered Nov 16 '22 08:11

Vinicius Lobo Silva


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

like image 44
ilke kalkan Avatar answered Nov 16 '22 07:11

ilke kalkan


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;
    }
like image 1
Omid Soleiman Avatar answered Nov 16 '22 07:11

Omid Soleiman