如何将多个起点每个起点都有自己的多个目的地发送到查询字符串上的Google Distance Matrix

问题描述

我正在使用C#WebClient和UriBuilder类,并与Google distance Matrix API通信。我的程序正在发送以逗号分隔的纬度,经度对。事情在一个起点和多个目的地的情况下正常工作:查询字符串上的目的地值只需要用竖线字符“ |”定界:

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

但是我想让它与多个起点一起工作,其中每个起点都有自己的多个目的地。那可能吗?还是API会产生笛卡尔乘积,计算每个起点到每个目的地的距离?

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

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

destinations array shown in debugger

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

解决方法

有可能,但是Google Distance Matrix 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
        },}

因此,将根据origins参数中的原点顺序对行进行排序。每行元素内部均根据destinations参数中目标的顺序进行排序。