0

我正在使用 C# WebClient 和 UriBuilder 类,与 Google 距离矩阵 API 进行通信。我的程序正在发送以逗号分隔的纬度、经度对。一个起点和多个目的地都可以正常工作:查询字符串上的目的地值只需要用竖线字符“|”分隔:

    &destinations=latitude1,longitude1|latitude2,longitude2... 

但我想让它与多个来源一起工作,每个来源都有自己的多个目的地。那可能吗?或者 API 是否生成笛卡尔积,计算每个起点到每个目的地的距离?

如果可能的话,如何在查询字符串上origins[i]进行协调?destinations[i]

这是我的 C# 程序中的示例结构(地理位置被遮挡):

调试器中显示的目标数组

我需要将该结构转换为 API 将在查询字符串上接受的格式,以destinationArray[0]origin[0]destinationArray[1]with链接的方式origin[1]

4

1 回答 1

1

这是可能的,但谷歌距离矩阵 API 会给你比你需要的更多的结果。所以是的,它确实产生了笛卡尔积,但你可以从结果中提取你需要的东西。除了为每个来源发送单独的请求之外,别无他法。

此处记录了响应的结构https://developers.google.com/maps/documentation/distance-matrix/overview#distance-matrix-responses

TLDR 版本如下:

要求:

origins: o1|o2
destinations: d1|d2

结果将按以下方式结构化/排序:

{
  // stuff removed for brevity
  // ...
  "rows": [
    // row for the first origin (o1)
    {
      "elements": [
        // element for the first destination (d1)
        {
          // stuff removed for brevity
        },
        // element for the second destination (d2)
        {
          // stuff removed for brevity
        }
      ]
    },
    // row for the second origin (o2)
    {
      "elements": [
        // element for the first destination (d1)
        {
          // stuff removed for brevity
        },
        // element for the second destination (d2)
        {
          // stuff removed for brevity
        }
      ]
    },
}

因此,行是根据参数中原点的顺序进行排序的origins每行内的元素根据destinations参数中目的地的顺序进行排序。

于 2020-09-03T14:54:03.470 回答