【问题标题】:Google map api v3: find multiple paths using DistanceMatrixServiceGoogle map api v3:使用 DistanceMatrixService 查找多条路径
【发布时间】:2015-09-09 12:32:49
【问题描述】:

我正在使用 Google map api 在 DistanceMatrixService() 的帮助下查找两点之间的距离。在getDistanceMatrix()的成功回调中,我可以得到两点之间的距离。两点之间可能有多条路径,但我总是得到最短路径的长度。我怎么能得到所有的路径距离?请参考以下代码:

var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
    origins: [--some origins--],
    destinations: [--some destinations--],
    travelMode: google.maps.TravelMode.DRIVING,
    unitSystem: google.maps.UnitSystem.METRIC,
    avoidHighways: false,
    avoidTolls: false,
}, function (response, status) {
        for (var i = 0, length = response.rows[0].elements.length; i < length; i++) {
            if (response.rows[0].elements[i].status == 'NOT_FOUND') {
                alert('Source or destination address could not be found.');
                return;
            }

            var distance = response.rows[0].elements[i].distance.text;
            var duration = response.rows[0].elements[i].duration.text;
        }
});

在上面的代码中,数组response.rows[0].elements 总是长度为1。如果源和目标之间有多个路径,它应该有多个对象。如何做到这一点?提前致谢。

【问题讨论】:

标签: javascript google-maps google-maps-api-3 web


【解决方案1】:

DistanceMatrix 不允许多条路线,它总是计算最短/最佳距离。

如果您需要备用路线并且只有一个起点和终点,请使用 DirectionsService,将 provideRouteAlternatives 设置为 true

来自the documentation

provideRouteAlternatives(可选)设置为 true 时指定路线服务可以在响应中提供多个替代路线。请注意,提供替代路由可能会增加服务器的响应时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    相关资源
    最近更新 更多