如何使用Google距离矩阵通过特定点找到起点和终点之间的距离

时间:2019-03-31 13:38:24

标签: javascript google-distancematrix-api distance-matrix

我想知道如何使用Google Distance Matrix API通过JavaScript中的特定点计算起点和终点之间的距离。

所以在这里让我解释一下,假设起点是Origin_1,终点是destination_1,并且在它们之间有多个点(为简单起见,我们假设它们之间只有3个点,它们分别是point_1,point_2和point_3)。 / p>

所以我想找到如下距离:

distance_1 = Origin_1 to point_1
distance_2 = point_1 to point_2
distance_3 = point_2 to point_3
distance_4 = point_3 to destination_1

Total distance = distance_1 + distance_2 + distance_3 + distance_4

已检查距离矩阵API参考Link,但找不到任何东西。那么我应该在以下代码中进行哪些更改。这样它将按照上面的说明工作。

var service = new google.maps.DistanceMatrixService();
           service.getDistanceMatrix(
              {
                origins: [origin_1, point_1, point_2, point_3],
                destinations: [point_1, point_2, point_3, destination_1],
                travelMode: "DRIVING",
                unitSystem: google.maps.UnitSystem.METRIC,
                avoidHighways: false,
                avoidTolls: false
              }




    for (var i = 0; i < originList.length; i++) {
                    var results = response.rows[i].elements;
                    geocoder.geocode(
                      { address: originList[i] },
                      showGeocodedAddressOnMap(false)
                    );
                    for (var j = 0; j < results.length; j++) {
                      geocoder.geocode(
                        { address: destinationList[j] },
                        showGeocodedAddressOnMap(true)
                      );
                      outputDiv.innerHTML +=
                        originList[i] +
                        " to " +
                        destinationList[j] +
                        ": " +
                        results[j].distance.text +
                        " in " +
                        results[j].duration.text +
                        "<br>";
                    }
                  }

0 个答案:

没有答案