Google map API v3:使用DistanceMatrixService查找多个路径 [英] Google map api v3: find multiple paths using DistanceMatrixService

查看:553
本文介绍了Google map API v3:使用DistanceMatrixService查找多个路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在DistanceMatrixService()的帮助下,我使用Google map api来查找两点之间的距离。在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('找不到源地址或目的地址');
return;
}

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

在上面的代码中,数组 response.rows [0] .elements 总是变成一个长度。如果源和目标之间有多条路径,它应该有更多的对象。如何实现这一目标? / distancematrixrel =nofollow> DistanceMatrix 不允许多个路线,它总是计算最短距离/最佳距离。



如果需要替代路线并且只有一个出发地和目的地,请使用 DirectionsService ,将 provideRouteAlternatives true



from 文档

$ b $当设置为true时,b

provideRouteAlternatives(可选)指定路线服务可以在响应中提供多个路线备选项。请注意,提供路由选择可能会增加服务器的响应时间。



I am using Google map api for finding distance between two points with the help of DistanceMatrixService(). In the success callback of getDistanceMatrix(), I can get the distance between two points. There could be multiple paths between two points but I always get the length of shortest path. How could I get all path distances? Please refer the code below:

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;
        }
});

In above code, the array response.rows[0].elements always turns out to have length one. It should have more that one objects if there are multiple paths between source and destination. How to achieve that? Thanks in advance.

解决方案

The DistanceMatrix doesn't allow for multiple routes, it always calculates the shortest/best distance.

If you need alternate routes and only have a single origin and destination, user the DirectionsService, set provideRouteAlternatives to true.

from the documentation:

provideRouteAlternatives (optional) when set to true specifies that the Directions service may provide more than one route alternative in the response. Note that providing route alternatives may increase the response time from the server.

这篇关于Google map API v3:使用DistanceMatrixService查找多个路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆