【问题标题】:Bing Maps REST Toolkit - accessing RESPONSE objectBing Maps REST Toolkit - 访问 RESPONSE 对象
【发布时间】:2020-05-03 21:34:39
【问题描述】:

我无法实际访问此无证类中的成员: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”值等待在 response.ResourceSet[0].Resources[0] 中使用——但我无法找到它们。以下是调试器显示的内容:

快照 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】:

    结果证明解决方案相当简单,但至少对我来说,在 BingMapsRESTToolkit 项目的(缺乏)文档中没有推断出来。

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

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

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

    这有点难看,但确实有效。

    【讨论】:

      猜你喜欢
      • 2022-12-04
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      相关资源
      最近更新 更多