Search code examples
c#bing-mapsbing-api

missing a final step in using the BingMapsRESTToolkit to get a distance


I need to calculate the distance between 2 places, using the BinMapsRESTToolkit that can be found here.

However, the documentation only lists properties and methods, but there is no example on how to use this.

What I got so far is this

async Task<double> CalculateDistanceWithBingAPI(string from, string to, DataRow row)
{
    double Result = 0;

    DistanceMatrixRequest dm = new DistanceMatrixRequest();
    dm.BingMapsKey = Settings.BingKey;
    dm.TravelMode = TravelModeType.Truck;

    dm.Origins = new List<SimpleWaypoint>();
    dm.Destinations = new List<SimpleWaypoint>();

    dm.Origins.Add(new SimpleWaypoint(from));
    dm.Destinations.Add(new SimpleWaypoint(to));

    var x = await dm.Execute();

    //row["Distance"] = dm.??????

    return Result;
}

This executes without throwing an exception, but after executing the line var x = await dm.Execute(); the debugger jumps out of this procedure.

If I call it without the await then the variable x has a value "not computed yet".

I have no clue how to retrieve the actual distance from the object DistanceMatrixRequest after calling the Execute(). It has no events, it has no properties that could hold the info, and it has no usefull documentation

Either I am close but am missing a final step, or I am completely using a wrong approach here, so I could use a little help here.


Solution

  • You need to call GetEuclideanDistanceMatrix on your DistanceMatrixRequest to get DistanceMatrix and then distance should be available via DistanceMatrix.GetDistance.

    DistanceMatrixRequest request = new DistanceMatrixRequest();
    // ...
    DistanceMatrix matrix = await request.GetEuclideanDistanceMatrix();
    double distance = matrix.GetDistance(...);