microsoft / BingMapsRESTToolkit

This is a portable class library which makes it easy to access the Bing Maps REST services from .NET.

Home Page:https://github.com/Microsoft/BingMapsRESTToolkit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Travel directions using TravelModeType.Truck fail

topeju opened this issue · comments

Travel directions using TravelMode = TravelModeType.Truck seem to fail with an "Object reference not set to an instance of an object" error from the server.

C# code to reproduce:

        public static void TruckRouteTest()
        {
            var request = new RouteRequest {
                BingMapsKey = _ApiKey,
                RouteOptions = new RouteOptions {
                    //Avoid = new System.Collections.Generic.List<AvoidType> { },
                    DateTime = DateTime.Now,
                    //DistanceBeforeFirstTurn = 0,
                    DistanceUnits = DistanceUnitType.Kilometers,
                    //Heading = 270,
                    MaxSolutions = 3,
                    Optimize = RouteOptimizationType.TimeWithTraffic,
                    RouteAttributes = new System.Collections.Generic.List<RouteAttributeType> { RouteAttributeType.All },
                    TimeType = RouteTimeType.Departure,
                    //Tolerances = new System.Collections.Generic.List<double> {0.00000344, 0.0005978},
                    TravelMode = TravelModeType.Truck,
                    VehicleSpec = new VehicleSpec {
                        DimensionUnit = DimensionUnitType.Meter,
                        WeightUnit = WeightUnitType.Kilogram,
                        VehicleHeight = 2,
                        VehicleWidth = 2,
                        VehicleLength = 6,
                        VehicleWeight = 3901,
                        VehicleAxles = 2,
                        VehicleSemi = false,
                        VehicleTrailers = 0,
                        VehicleMaxGradient = 45,
                        VehicleMinTurnRadius = 8,
                        VehicleAvoidCrossWind = false,
                        VehicleAvoidGroundingRisk = false,
                        //VehicleHazardousMaterials = new System.Collections.Generic.List<HazardousMaterialType> { },
                        //VehicleHazardousPermits = new System.Collections.Generic.List<HazardousMaterialPermitType> { }
                    },
                },
                Waypoints = new System.Collections.Generic.List<SimpleWaypoint> {
                    new SimpleWaypoint { Coordinate = new Coordinate { Latitude = 60.420559, Longitude = 22.458193} },
                    new SimpleWaypoint { Address = "Betaniankatu 12, Turku" }
                },
                //BatchSize = 25,
                //Culture = "fi",
                //UserLocation = new Coordinate { Latitude = 60.420559, Longitude = 22.458193 },
                //UserRegion = "FI",
            };
            var resources = GetResourcesFromRequest(request);

            foreach (var resource in resources)
            {
                var r = resource as Route;
                if (r == null) {
                    Console.WriteLine("resource is not a Route, but a {0}: {1}", resource.GetType().FullName, resource.ToString());
                } else { 
                    Console.WriteLine("Distance: {0}, duration: {1}", r.TravelDistance, r.TravelDuration);
                }
            }

            Console.ReadLine();
        }

This will output resource is not a Route, but a BingMapsRESTToolkit.RouteProxyAsyncResult: BingMapsRESTToolkit.RouteProxyAsyncResult. Looking at the RouteProxyAsyncResult, its ErrorMessage indicates Object reference not set to an instance of an object. (but I have no idea which object reference is null).

The at least roughly equivalent GET request is https://dev.virtualearth.net/REST/v1/Routes/Truck?wayPoint.1=60.420559,22.458193&waypoint.2=Betaniankatu 12, Turku&optimize=timeWithTraffic&routeAttributes=routePath,regionTravelSummary&dateTime=2021-03-24T23:03:00&distanceUnit=km&vehicleHeight=2&vehicleWidth=2&vehicleLength=6&vehicleWeight=3901&vehicleAxles=2&vehicleTrailers=0&vehicleSemi=false&vehicleMaxGradient=45&vehicleMinTurnRadius=8&vehicleAvoidCrossWind=false&vehicleAvoidGroundingRisk=false&key={{bingMapsApiKey}}, and that does return a valid route.

If you change the TravelMode in the C# code to Driving, the request will also return the same route (which is valid for trucks at least of the size indicated). However, I can't get the request to work with different Optimize options either (as long as TravelMode is Truck).

The commented-out lines indicate options I have tried to set when trying to find which reference hasn't been set, but they do not make any difference in the response.

This may or may not be related or the same issue as #42, but that issue specifically refers to TimeWithTraffic which this doesn't require.

Digging into this, it appears the issue is the MaxSolutions option. The truck routing service doesn't support this, but other routing options do. When this service was added to this library, the truck routing service simply ignored this option so it was included incase it was ever supported in the future, looks like something changed over time. A quick fix is to simply remove this option from your request. I'll modify this library so this isn't added to the request.