如何找到多个标记之一的最佳路线? [英] How to find optimal route to one of many markers?

查看:108
本文介绍了如何找到多个标记之一的最佳路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在地图上,我必须使用标记数组。一个是静态的,我们称之为 station 。其他的也是静态的,但它们是临时的,我们称它们为 fire 。当点击 fire 时,最短路线必须构建为站点之一。我使用TravelMode.DRIVING。我知道我可以计算路线距离,但为了计算我必须先构建路线。是否有任何图书馆可以找到一个固定标记与许多其他标记之间的最佳路线?或者你能帮我讲逻辑吗?我可以自己编写代码。

这里是我对两个标记中的一个实现了最佳路径,并且可以使用循环计算许多标记。但是有没有其他办法可以做到这一点?

  directionsService1.route(request1,function(response1,status1){
if(status1 == google.maps。 DirectionsStatus.OK){
var myRoute1 = response1.routes [0] .legs [0];
distance1 = myRoute1.distance.value;
directionsService2.route(request2,function(response2,状态2){
if(status2 == google.maps.DirectionsStatus.OK){
var myRoute2 = response2.routes [0] .legs [0];
distance2 = myRoute2.distance。值;
if(distance1> distance2){
directionsDisplay2.setMap(map);
directionsDisplay2.setDirections(response2);
directionsDisplay1.setMap(null);
} else {
directionsDisplay1.setMap(map);
directionsDisplay1.setDirections(response1);
directionsDisplay2.setMap(NULL);
}
}
});
}
});


解决方案

路线服务受费率限制并受配额限制。如果你有很多积分,你将无法对它们进行循环调用。

一个建议是计算所有点的直线距离,然后计算最接近的8个结果的行驶距离(假设有8个以上的起点用)。我建议8,因为这可能是您可以放入方向要求的航点的最大数量,我想如果您将optimize_waypoints设置为true,并将所有8个航点添加为往返行程(起点和终点相同),你会得到第一个或最后一个航点和起点之间最短的驾驶距离。

另一个是使用谷歌的 distance matrix api


On a map I have to arrays of markers. One's are static, let's call station. Other's also static, but they are temporary, let's call them fire. When fire is clicked shortest route must be constructed to one of stations. I use TravelMode.DRIVING. I know that I can calculate route distance, but in order to calculate I must construct route first. Is there any library to find optimal route between one fixed marker and one of many other markers? Or can you help me telling logic? I can write code myself.

Here is I implemented optimal route to one of two markers, and to many markers can be calculated using loop. But is there any alternative way to do it?

    directionsService1.route(request1, function (response1, status1) {
        if (status1 == google.maps.DirectionsStatus.OK) {
            var myRoute1 = response1.routes[0].legs[0];
            distance1 = myRoute1.distance.value;
            directionsService2.route(request2, function (response2, status2) {
                if (status2 == google.maps.DirectionsStatus.OK) {
                    var myRoute2 = response2.routes[0].legs[0];
                    distance2 = myRoute2.distance.value;
                    if (distance1 > distance2) {
                        directionsDisplay2.setMap(map);
                        directionsDisplay2.setDirections(response2);
                        directionsDisplay1.setMap(null);
                    } else {
                        directionsDisplay1.setMap(map);
                        directionsDisplay1.setDirections(response1);
                        directionsDisplay2.setMap(null);
                    }
                }
            });
        }
    });

解决方案

The directions service is rate limited and subject to a quota. If you have lots of points you are not going to be able to call it in a loop for all of them.

One suggestion would be to calculate the straight line distance to all the points, then the driving distance to the closest 8 of those result (assuming there were more than 8 to start with). I suggest 8 as that would be the maximum number of waypoints you ca put in a directions request, I think if you set optimize_waypoints to true and add all of those 8 as waypoints to a round trip (start and end points are the same), you will get the shortest driving distance between either the first or the last waypoint and the start point.

Another would be to use google's distance matrix api

这篇关于如何找到多个标记之一的最佳路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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