GMap.NET route is returning null
Asked Answered
A

5

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.

Agnes answered 29/7, 2015 at 6:40 Comment(11)
Have you tried passing in the strings of the adresses?Gavage
@Gavage can you give me an example please?Agnes
try this ::if (route == null) { List lstPoints = new List {start ,end}; route = new MapRoute (lstPoints);Tussis
GoogleMapProvider.Instance.GetRoute("broadway, new york city", "15th street, new york city", false, false, 15);Gavage
@Gavage it also returns null plus I have to work with coordinates..Agnes
@Tussis it didn't work..Agnes
what does it mean "didnt work" ? U get null exception again??Tussis
@Tussis MapRoute only accepts MapRoute(string)Agnes
@nozzleman,sakir, I found the problem.. The routing service was been removed by google recently..Agnes
is there any workaround?Gavage
@Gavage If you don't need to get the route's distance value, you can use GetDirections. I couldn't manage to find any other solutions on WinForm using Google Map Service, but now I'm using BingMapProvider with Devexpress Extension on VSAgnes
A
5

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

Agnes answered 30/7, 2015 at 11:28 Comment(1)
What is the replacment then?Fellowship
M
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...

Money answered 29/7, 2015 at 19:25 Comment(4)
I was trying to get the distance from the route, I don't need directions but thanks for suggestionAgnes
@Vinicious Did this code work for you?On codeplex forums they told me that google has removed routing service. linkAgnes
this give Object reference not set to an instance of an object.' on this line: GMapRoute r = new GMapRoute(ss.Route, "My route");Fellowship
This gives me the same error - ss ends up being null.Lock
A
5

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

Agnes answered 30/7, 2015 at 11:28 Comment(1)
What is the replacment then?Fellowship
M
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;
    }
Mirabel answered 5/7, 2019 at 21:17 Comment(0)
M
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

Mandeville answered 18/3, 2016 at 14:19 Comment(0)
W
0

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

Wretch answered 14/6, 2021 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.