本文介绍了Google Maps Api直(最短)路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试找到一种方法来使用Google Maps Api V3进行直线导航.

I am currently trying to find a way how to get a straight route with Google Maps Api V3.

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

I already managed it to use geocode and the directions service to get a route from point A to point B, including two alternative routes. I also experimented with "No highways" and "No tolls" but nothing appears to solve this problem entirely...At the moment I check the three given routes for lowest miles but this has to be proven not really to be the shortest route.

我不是不是搜索最快或最快的路线,而只是搜索直线并尽可能低英里.

I am not searching the fastest or quickest route but just a straight route with as low miles as possible.

由于我没有找到任何线索,因此使用Google解释了类似我需要我问的问题.也许有人在这里有解决方案...

As I did not find any thread by using google explaining something like I need I ask you. Maybe somebody has a solution here...

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

P.S.: I also can't use the "Pedestrian Mode" as this is used as a navigation help for our local fire trucks when our mounted navigation systems do not work again.This is also the reason why we need as low kilometers as possible - when driving a firetruck round here the fastest route is 99% the one with lowest miles but the Api won't let me decide that and insist on using principal roads

推荐答案

要获得从A到BI的最短路径,建议使用"alternatives = true"参数进行不同的查询,并在避免之间使用"avoid"参数=收费,避免=高速公路,然后我将比较所有结果以选择最短的路线.

To obtain the shortest route from A to B I would suggest to make different queries with the "alternatives=true" parameter, playing with the "avoid" parameter between avoid=toll, avoid=highways, and then I would compare all results to pick the shortest route.

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

    }

这篇关于Google Maps Api直(最短)路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:53