Bing Maps REST工具包-访问RESPONSE对象

问题描述 投票:0回答:1

我无法实际访问此未记录的类中的成员:BingMapsRESTToolkit.Response

[各个地方的代码(例如)Bing Maps REST Services Toolkit - Access Value Outside of Delegate似乎从MSDN复制了无效的示例。

例如,此方法无法访问Response对象中包含的Snapped坐标数组。将rez分配给响应的类型所在的行为空。

private List<Coordinate> MapSnaps(List<Coordinate> coordinates)
{
    // Build our request object
    var req = new SnapToRoadRequest();
    req.BingMapsKey = _sessionKey;
    req.UserRegion = "US";
    req.SpeedUnit = SpeedUnitType.MPH;
    req.TravelMode = TravelModeType.Driving;
    req.Points = coordinates;
    var response = req.Execute().GetAwaiter().GetResult();

    if(response != null &&
       response.ResourceSets != null &&
       response.ResourceSets.Length > 0 &&
       response.ResourceSets[0].Resources != null &&
       response.ResourceSets[0].Resources.Length > 0)
    {
        // rez gets nothing, results in null because the two objects are nothing alike
        var rez = response.ResourceSets[0].Resources[0] as BingMapsRESTToolkit.Location;

        // Snaps has 69 SnappedPoint items, but the member as shown in the debug window
        // SnappedPoints under snaps cant be accessed in code - just in debugger
        var snaps = response.ResourceSets[0].Resources[0]; // 

        Console.WriteLine("-.-"); // <-- breakpoint here

    }

    // TODO: Once we get this response snafu sorted, convert and return
    // matching list of road-snapped coordinates to caller...

    return ConvertToCoords(response);
}

[当我使用Visual Studio 2019进行调试时,它清楚地表明有69个'SnappedPoint'值正在等待响应中使用。ResourceSet[0] .Resources [0]-但我无法理解。这是调试器显示的内容:

snaps BingMapsRESTToolkit.SnappedPoint [69]

SnappedPoints   BingMapsRESTToolkit.SnappedPoint[69]
BoundingBox     null
Type            null

Each member of the SnappedPoints array contains:

    Coordinate          {Lat:double, Lon:double}
    Index               int
    Name                String for name of road
    SpeedLimit          double?
    TruckSpeedLimit     double?

正在尝试:snaps.SnappedPoints [x]不起作用,因为该成员不存在。 BoundingBox和Type都可以访问,并且都设置为null,但是我看不到访问SnappedPoints内容的方法。没有智能,手动输入成员名称只会导致错误。

我在这里错了吗?

rest bing-maps
1个回答
0
投票
事实证明,该解决方案相当简单,但至少对我而言,这不是从BingMapsRESTToolkit项目的(缺乏)文档中推断出来的。

Response对象可以根据初始请求采用多种对象类型。

为了访问SnappedPoints数组,必须将Response强制转换为SnapToRoadResponse类型,例如:

var snaps = response.ResourceSets[0].Resources[0] as SnapToRoadResponse;

这有点丑陋,但确实有效。 
© www.soinside.com 2019 - 2024. All rights reserved.