缺少使用 BingMapsRESTToolkit 获取距离的最后一步

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

我需要计算 2 个地方之间的距离,使用可以找到的 BinMapsRESTToolkit here

然而,文档只列出了属性和方法,但没有关于如何使用它的示例。

到目前为止我得到的是这个

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;
}

执行时不会抛出异常,但在执行行 var x = await dm.Execute(); 后,调试器会跳出此过程。

如果我在没有 await 的情况下调用它,那么变量 x 的值为 "not computed yet"。

我不知道如何在调用 Execute() 后从对象 DistanceMatrixRequest 检索实际距离。它没有事件,没有可以保存信息的属性,也没有有用的文档

要么我很接近但缺少最后一步,要么我在这里完全使用了错误的方法,所以我需要一些帮助。

您需要在您的 DistanceMatrixRequest 上调用 GetEuclideanDistanceMatrix 以获得 DistanceMatrix 然后应该可以通过 DistanceMatrix.GetDistance.

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