谷歌地图Api直(最短)路线

use*_*424 6 google-maps routes google-maps-api-3

我目前正试图找到一种如何使用Google Maps Api V3获得直线路线的方法.

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

不是在寻找最快或最快的路线,而只是寻找尽可能低的英里路线.

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

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

Sol*_*tos 1

要获得从 A 到 BI 的最短路线,建议使用 \xe2\x80\x9calternatives=true\xe2\x80\x9d 参数进行不同的查询,并使用 \xe2\x80\x9cavoid\xe2\x80\x9d 参数避免=收费站,避免=高速公路,然后我会比较所有结果以选择最短路线。

\n\n
 directionsService = new google.maps.DirectionsService;\n//avoiding tolls\n            directionsService.route({\n                origin: {\n                    \'placeId\': originId\n                },\n                destination: {\n                    \'placeId\': destinationId\n                },\n                provideRouteAlternatives: true,\n                avoidTolls: true,\n                travelMode: google.maps.TravelMode.DRIVING\n            }, function(response, status) {\n                if (status === google.maps.DirectionsStatus.OK) {\n                    routesResponses.push(response);\n                }\n                else {\n                    window.alert(\'Directions request failed due to \' + status);\n                }\n            });\n            //avoiding highways\n            directionsService.route({\n                origin: {\n                    \'placeId\': originId\n                },\n                destination: {\n                    \'placeId\': destinationId\n                },\n                provideRouteAlternatives: true,\n                avoidHighways: true,\n                travelMode: google.maps.TravelMode.DRIVING\n            }, function(response, status) {\n                if (status === google.maps.DirectionsStatus.OK) {\n                    routesResponses.push(response);\n                }\n                else {\n                    window.alert(\'Directions request failed due to \' + status);\n                }\n\n                //Results analysis and drawing of routes\n                var fastest = Number.MAX_VALUE,\n                    shortest = Number.MAX_VALUE;\n\n                routesResponses.forEach(function(res) {\n                    res.routes.forEach(function(rou, index) {\n                        console.log("distance of route " +index+": " , rou.legs[0].distance.value);\n                        console.log("duration of route " +index+": " , rou.legs[0].duration.value);\n                        if (rou.legs[0].distance.value < shortest) shortest = rou.legs[0].distance.value  ;\n                        if (rou.legs[0].duration.value < fastest) fastest = rou.legs[0].duration.value  ;\n\n                    })\n                })\n                console.log("shortest: ", shortest);\n                console.log("fastest: ", fastest);\n//painting the routes in green blue and red\n                 routesResponses.forEach(function(res) {\n                    res.routes.forEach(function(rou, index) {\n                        new google.maps.DirectionsRenderer({\n                            map:map,\n                            directions:res,\n                            routeIndex:index,\n                            polylineOptions:{\n                                strokeColor: rou.legs[0].duration.value == fastest? "red":rou.legs[0].distance.value == shortest?"darkgreen":"blue",\n                                strokeOpacity: rou.legs[0].duration.value == fastest? 0.8:rou.legs[0].distance.value == shortest? 0.9: 0.5,\n                                strokeWeight: rou.legs[0].duration.value == fastest? 9:rou.legs[0].distance.value == shortest? 8: 3,\n                            }\n                        })\n                    })\n                })   \n            });\n        }\n\n    }\n
Run Code Online (Sandbox Code Playgroud)\n