【问题标题】:Google Maps Api straight (shortest) routeGoogle Maps Api 直线(最短)路线
【发布时间】:2013-08-03 21:49:40
【问题描述】:

我目前正在尝试使用 Google Maps Api V3 找到一条直线路线。

我已经设法使用地理编码和路线服务来获取从 A 点到 B 点的路线,包括两条替代路线。我还尝试了“无高速公路”和“无通行费”,但似乎没有什么能完全解决这个问题...... 目前我检查了三个给定路线的最低里程,但必须证明这并不是最短的路线。

不是搜索最快或最快的路线,而只是寻找一条直路尽可能低里程

由于我没有找到 任何 线程通过使用谷歌解释我需要的东西我问你。也许有人在这里有解决方案...

P.S.:我也不能使用“行人模式”,因为当我们安装的导航系统不再工作时,它会用作我们当地消防车的导航帮助。 这也是我们需要尽可能低公里数的原因 - 在这里驾驶救火车时,最快的路线是 99% 的里程数最低的路线,但 Api 不会让我决定并坚持使用主要道路

【问题讨论】:

  • 你找到解决办法了吗
  • Soldeplata Saketos 的回答似乎是目前最好的解决方案。这不是我所期望的,但我认为这是我们目前能得到的最接近的结果。

标签: google-maps google-maps-api-3 routes


【解决方案1】:

要获得从 A 到 BI 的最短路线,建议使用“alternatives=true”参数进行不同的查询,在避免=toll、避免=高速公路之间使用“避免”参数,然后我会比较所有结果选择最短的路线。

 directionsService = new google.maps.DirectionsService;
//avoiding tolls
            directionsService.route({
                origin: {
                    'placeId': originId
                },
                destination: {
                    'placeId': destinationId
                },
                provideRouteAlternatives: true,
                avoidTolls: true,
                travelMode: google.maps.TravelMode.DRIVING
            }, function(response, status) {
                if (status === google.maps.DirectionsStatus.OK) {
                    routesResponses.push(response);
                }
                else {
                    window.alert('Directions request failed due to ' + status);
                }
            });
            //avoiding highways
            directionsService.route({
                origin: {
                    'placeId': originId
                },
                destination: {
                    'placeId': destinationId
                },
                provideRouteAlternatives: true,
                avoidHighways: true,
                travelMode: google.maps.TravelMode.DRIVING
            }, function(response, status) {
                if (status === google.maps.DirectionsStatus.OK) {
                    routesResponses.push(response);
                }
                else {
                    window.alert('Directions request failed due to ' + status);
                }

                //Results analysis and drawing of routes
                var fastest = Number.MAX_VALUE,
                    shortest = Number.MAX_VALUE;

                routesResponses.forEach(function(res) {
                    res.routes.forEach(function(rou, index) {
                        console.log("distance of route " +index+": " , rou.legs[0].distance.value);
                        console.log("duration of route " +index+": " , rou.legs[0].duration.value);
                        if (rou.legs[0].distance.value < shortest) shortest = rou.legs[0].distance.value  ;
                        if (rou.legs[0].duration.value < fastest) fastest = rou.legs[0].duration.value  ;

                    })
                })
                console.log("shortest: ", shortest);
                console.log("fastest: ", fastest);
//painting the routes in green blue and red
                 routesResponses.forEach(function(res) {
                    res.routes.forEach(function(rou, index) {
                        new google.maps.DirectionsRenderer({
                            map:map,
                            directions:res,
                            routeIndex:index,
                            polylineOptions:{
                                strokeColor: rou.legs[0].duration.value == fastest? "red":rou.legs[0].distance.value == shortest?"darkgreen":"blue",
                                strokeOpacity: rou.legs[0].duration.value == fastest? 0.8:rou.legs[0].distance.value == shortest? 0.9: 0.5,
                                strokeWeight: rou.legs[0].duration.value == fastest? 9:rou.legs[0].distance.value == shortest? 8: 3,
                            }
                        })
                    })
                })   
            });
        }

    }

【讨论】:

    【解决方案2】:

    正如我所说的here,我使用了一个解决方案,它调用一次路由服务并以您认为有用的方式过滤结果。在我的示例中,我正在计算最短路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-15
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多