Javascript Google地图方向渲染备选路线如何选择所需路径

Javascript Google地图方向渲染备选路线如何选择所需路径,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我正在研究谷歌地图方向,我正在关注谷歌的导航应用程序 我可以通过DirectionsService获得所有可能的备选路线,并可以为多段线提供不同的颜色。我希望用户能够通过单击多段线来选择所需的路径。有些人还没有找到任何相关的方法 我的代码: directionsService.route(request, function(response, status) { var points = []; if (status == google.maps.DirectionsStatus.OK)

我正在研究谷歌地图方向,我正在关注谷歌的导航应用程序

我可以通过DirectionsService获得所有可能的备选路线,并可以为多段线提供不同的颜色。我希望用户能够通过单击多段线来选择所需的路径。有些人还没有找到任何相关的方法

我的代码:

directionsService.route(request, function(response, status) {
  var points = [];
  if (status == google.maps.DirectionsStatus.OK) {
    try {
      var polycolour = "";
      var Opacity = 0;
      //var PolyLine = '';
      for (var i = 0, len = response.routes.length; i < len; i++) {
        if (i == 0) {
          polycolour = "Blue";
          Opacity = 5;
        }
        else {
          polycolour = "grey";
          Opacity = 2;
        }
        directionsDisplay = new google.maps.DirectionsRenderer({
          map: map,
          directions: response,
          routeIndex: i,
          draggable : true,
          polylineOptions: {
            strokeColor: polycolour,
            strokeWeight: Opacity
          }
        });

        var infowindow2 = new google.maps.InfoWindow();
        //var step = 10;
        //alert(angular.toJson(response.routes[0].legs[0].steps[i]));

        infowindow2.setContent(""+((response.routes[i].legs[0].distance.value)/1000)+" KM");
        infowindow2.setPosition(response.routes[i].legs[0].steps[8].end_location);
        infowindow2.open(map);

      }
      //directionsDisplay.setMap(map);
      google.maps.event.addListener(directionsDisplay, 'click', function(){
        alert("helo");
      });
      //for (var k = 0, len = response.routes.length; k < len; k++) {
        //var myRoute = response.routes[k].legs[0];
        //for (var i = 0; i < myRoute.steps.length; i++) {
          //for (var j = 0; j < myRoute.steps[i].lat_lngs.length; j++) {
            // points.push(myRoute.steps[i].lat_lngs[j]);
          //}
        //}

        //var routLine = new google.maps.Polyline(
        //{
          //path: points,
          //strokeColor: "Red",
          //strokeOpacity: 0.5,
          // strokeWeight: 10
        // }
        // );
      // }
      // routLine.setMap(map)

      // Add a listener for the rightclick event on the routLine
      //google.maps.event.addListener(routLine, 'click', function(e){
      //try {
        //alert(angular.toJson(e));
      //}
      //catch (err)
      //{
        //  alert(err);
      //}
   // });

   //alert(angular.toJson(response.routes[0].legs[0].steps));
   //google.maps.event.addListener(PolyLine, 'routeindex_changed', function() {
     //alert("Bingo");
     //computeTotalDistance(directionsDisplay.getRouteIndex());
   //});
   //alert(response.routes.length);
   //directionsDisplay.setDirections(response);
   }
   catch (err)
   {
     alert(err);
   }
 }
});
directionsService.route(请求、功能(响应、状态){
var点=[];
if(status==google.maps.directionstatus.OK){
试一试{
var polycolour=“”;
var不透明度=0;
//变量多段线=“”;
对于(变量i=0,len=response.routes.length;i
首先,您需要告诉请求您想要其他路线,如下所示

// for example
var request = {
  origin: start,
  destination: end,
  provideRouteAlternatives: true,               
  travelMode: google.maps.TravelMode[DRIVING]
};
然后您有多个response.routes对象(注意,有时您只得到一个route)

现在您可以使用response.routes[i]作为方向渲染的源。
或者您可以创建自己的多段线。使用response.routes[i].overview\u路径作为路径

var line = new google.maps.Polyline({
  path: response.routes[i].overview_path,
  strokeColor: "#ff0000",  // you might want different colors per suggestion
  strokeOpacity: 0.7,
  strokeWeight: 3
});
line.setMap(map);

下面是一个运行示例。 只需更改API密钥

按照您的要求,单击路线会高亮显示该路线

更新:我喜欢这样

生成灰线和彩色线。但高亮显示仅显示地图上的一条建议

大灰线很好点击。因此,它得到的是单击事件,而不是彩色线

这也是避免Z索引问题的最简单方法

我存储数据(持续时间、距离),显示在信息窗口上

<!DOCTYPE html>
<html>
<head>
    <title>Google Map Direction Render Alternate Route How To Select Desired Path</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
        html, body {
            height: 100%;
            margin: 0;
            padding: 0;
        }
        #map {
            height: 90%;
        }
    </style>
</head>
<body>
    <form id="form">
        <input id="from" placeholder="From" value="Brussel" />
        <input id="to" placeholder="To" value="Antwerpen" />
        <input type="submit" value="GO"  />
    </form>

    <div id="map"></div>
    <div id="info">
        <a href="http://stackoverflow.com/questions/39264760/google-map-direction-render-alternate-route-how-to-select-desired-path/39268344#39268344">Stackoverflow</a>
    </div>
    <script type="text/javascript" src="https://maps.google.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry"></script>
    <script>
        var map;
        var directionsService;
        var polylines = [];
        var shadows = [];
        var data = [];
        var infowindow;
        function initMap() {
            map = new google.maps.Map(document.getElementById('map'), {
                center: {lat: 50.84659376378408, lng: 4.3531406857355215},
                zoom: 12,
                mapTypeId: 'terrain'
            });
            google.maps.event.addDomListener(document.getElementById('form'), 'submit', function(e) {
                calcRoute(
                    document.getElementById('from').value,
                    document.getElementById('to').value
                );
                // prevent the form from really submitting
                e.preventDefault();
                return false;
            });
            directionsService = new google.maps.DirectionsService();
            // get the bounds of the polyline
            // http://stackoverflow.com/questions/3284808/getting-the-bounds-of-a-polyine-in-google-maps-api-v3
            google.maps.Polyline.prototype.getBounds = function(startBounds) {
                if(startBounds) {
                    var bounds = startBounds;
                }
                else {
                    var bounds = new google.maps.LatLngBounds();
                }
                this.getPath().forEach(function(item, index) {
                    bounds.extend(new google.maps.LatLng(item.lat(), item.lng()));
                });
                return bounds;
            };
        }
        // this function calculates multiple suggested routes.
        // We will draw 3 (broad stroke) suggested routs in grey.  These are broad to click on them easier.
        // We duplicate these routes with a thin, colored line; only route 0 is shown.
        function calcRoute(start, end) {
            var request = {
                origin: start,
                destination: end,
                provideRouteAlternatives: true,
                unitSystem: google.maps.UnitSystem.METRIC,
                travelMode: google.maps.TravelMode['DRIVING']
            };
            directionsService.route(request, function(response, status) {
                // clear former polylines
                for(var j in  polylines ) {
                    polylines[j].setMap(null);
                    shadows[j].setMap(null);
                }
                polylines = [];
                shadows = [];
                data = [];
                if (status == google.maps.DirectionsStatus.OK) {
                    var bounds = new google.maps.LatLngBounds();
                    for(var i in response.routes) {
                        // let's make the first suggestion highlighted;
                        var hide = (i==0 ? false : true);
                        var shadow = drawPolylineShadow(response.routes[i].overview_path, '#666666');
                        var line = drawPolyline(response.routes[i].overview_path, '#0000ff', hide);
                        polylines.push(line);
                        shadows.push(shadow);
                        // let's add some data for the infoWindow
                        data.push({
                            distance: response.routes[i].legs[0].distance,
                            duration: response.routes[i].legs[0].duration,
                            end_address: response.routes[i].legs[0].end_address,
                            start_address: response.routes[i].legs[0].start_address,
                            end_location: response.routes[i].legs[0].end_location,
                            start_location: response.routes[i].legs[0].start_location
                        });
                        bounds = line.getBounds(bounds);
                        google.maps.event.addListener(shadow, 'click', function(e) {
                            // detect which route was clicked on
                            var index = shadows.indexOf(this);
                            highlightRoute(index, e);
                        });

                    }
                    map.fitBounds(bounds);
                }
            });
        }
        // this makes one of the colored routes visible.
        function highlightRoute(index, e) {
            for(var j in  polylines ) {
                if(j==index) {
                    //var color = '#0000ff';
                    polylines[j].setMap(map);
                    // feel free to customise this string
                    var contentString =
                        '<span>'+ data[j].distance.text +'</span><br/>'+
                        '<span>'+ data[j].duration.text +'</span><br/>'+
                        '<span>route: '+ j +'</span><br/>'+
                        //'From: <span>'+ data[j].start_address +'</span><br/>'+
                        //'To: <span>'+ data[j].end_address +'</span><br/>'+
                        '';
                    if(e) {
                       var position = new google.maps.LatLng(e.latLng.lat(), e.latLng.lng());
                        // it may be needed to close the previous infoWindow
                        if(infowindow) {
                            infowindow.close();
                            infowindow = null;
                        }
                        infowindow = new google.maps.InfoWindow({
                            content: contentString,
                            position: position,
                            map: map
                        });
                        //infowindow.open(map, polylines[j]);
                    }
                }
                else {
                    polylines[j].setMap(null);
                }
            }
        }
        // returns a polyline.
        // if hide is set to true, the line is not put on the map
        function drawPolyline(path, color, hide) {
            var line = new google.maps.Polyline({
                path: path,
                strokeColor: color,
                strokeOpacity: 0.9,
                strokeWeight: 3
            });
            if(! hide) {
                line.setMap(map);
            }
            return line;
        }
        function drawPolylineShadow(path, color, hide) {
            var line = new google.maps.Polyline({
                path: path,
                strokeColor: color,
                strokeOpacity: 0.4,
                strokeWeight: 7
            });
            if(! hide) {
                line.setMap(map);
            }
            return line;
        }
        google.maps.event.addDomListener(window, 'load', initMap);
    </script>
</body>
</html> 

Google地图方向渲染备选路线如何选择所需路径
html,正文{
身高:100%;
保证金:0;
填充:0;
}
#地图{
身高:90%;
}
var映射;
var定向服务;
var多段线=[];
var阴影=[];
var数据=[];
var信息窗口;
函数initMap(){
map=new google.maps.map(document.getElementById('map'){
中心:{lat:50.84659376378408,lng:4.3531406857355215},
缩放:12,
mapTypeId:'地形'
});
google.maps.event.addDomListener(document.getElementById('form'),'submit',函数(e){
卡尔克劳特(
document.getElementById('from')。值,
document.getElementById('to').value
);
//防止表单真正提交
e、 预防默认值();
返回false;
});
directionsService=new google.maps.directionsService();
//获取多段线的边界
// http://stackoverflow.com/questions/3284808/getting-the-bounds-of-a-polyine-in-google-maps-api-v3
google.maps.Polyline.prototype.getBounds=函数(startBounds){
if(起始边界){
var界限=起始界限;
}
否则{
var bounds=new google.maps.LatLngBounds();
}
this.getPath().forEach(函数(项,索引){
extend(新的google.maps.LatLng(item.lat(),item.lng());
});
返回边界;
};
}
//此函数用于计算多条建议路线。
//我们将以灰色绘制3条(宽划)建议路线。这些都是广泛的点击它们更容易。
//我们用一条细细的彩色线条复制这些路线;仅显示路线0。
功能计算(启动,en
<!DOCTYPE html>
<html>
<head>
    <title>Google Map Direction Render Alternate Route How To Select Desired Path</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
        html, body {
            height: 100%;
            margin: 0;
            padding: 0;
        }
        #map {
            height: 90%;
        }
    </style>
</head>
<body>
    <form id="form">
        <input id="from" placeholder="From" value="Brussel" />
        <input id="to" placeholder="To" value="Antwerpen" />
        <input type="submit" value="GO"  />
    </form>

    <div id="map"></div>
    <div id="info">
        <a href="http://stackoverflow.com/questions/39264760/google-map-direction-render-alternate-route-how-to-select-desired-path/39268344#39268344">Stackoverflow</a>
    </div>
    <script type="text/javascript" src="https://maps.google.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry"></script>
    <script>
        var map;
        var directionsService;
        var polylines = [];
        var shadows = [];
        var data = [];
        var infowindow;
        function initMap() {
            map = new google.maps.Map(document.getElementById('map'), {
                center: {lat: 50.84659376378408, lng: 4.3531406857355215},
                zoom: 12,
                mapTypeId: 'terrain'
            });
            google.maps.event.addDomListener(document.getElementById('form'), 'submit', function(e) {
                calcRoute(
                    document.getElementById('from').value,
                    document.getElementById('to').value
                );
                // prevent the form from really submitting
                e.preventDefault();
                return false;
            });
            directionsService = new google.maps.DirectionsService();
            // get the bounds of the polyline
            // http://stackoverflow.com/questions/3284808/getting-the-bounds-of-a-polyine-in-google-maps-api-v3
            google.maps.Polyline.prototype.getBounds = function(startBounds) {
                if(startBounds) {
                    var bounds = startBounds;
                }
                else {
                    var bounds = new google.maps.LatLngBounds();
                }
                this.getPath().forEach(function(item, index) {
                    bounds.extend(new google.maps.LatLng(item.lat(), item.lng()));
                });
                return bounds;
            };
        }
        // this function calculates multiple suggested routes.
        // We will draw 3 (broad stroke) suggested routs in grey.  These are broad to click on them easier.
        // We duplicate these routes with a thin, colored line; only route 0 is shown.
        function calcRoute(start, end) {
            var request = {
                origin: start,
                destination: end,
                provideRouteAlternatives: true,
                unitSystem: google.maps.UnitSystem.METRIC,
                travelMode: google.maps.TravelMode['DRIVING']
            };
            directionsService.route(request, function(response, status) {
                // clear former polylines
                for(var j in  polylines ) {
                    polylines[j].setMap(null);
                    shadows[j].setMap(null);
                }
                polylines = [];
                shadows = [];
                data = [];
                if (status == google.maps.DirectionsStatus.OK) {
                    var bounds = new google.maps.LatLngBounds();
                    for(var i in response.routes) {
                        // let's make the first suggestion highlighted;
                        var hide = (i==0 ? false : true);
                        var shadow = drawPolylineShadow(response.routes[i].overview_path, '#666666');
                        var line = drawPolyline(response.routes[i].overview_path, '#0000ff', hide);
                        polylines.push(line);
                        shadows.push(shadow);
                        // let's add some data for the infoWindow
                        data.push({
                            distance: response.routes[i].legs[0].distance,
                            duration: response.routes[i].legs[0].duration,
                            end_address: response.routes[i].legs[0].end_address,
                            start_address: response.routes[i].legs[0].start_address,
                            end_location: response.routes[i].legs[0].end_location,
                            start_location: response.routes[i].legs[0].start_location
                        });
                        bounds = line.getBounds(bounds);
                        google.maps.event.addListener(shadow, 'click', function(e) {
                            // detect which route was clicked on
                            var index = shadows.indexOf(this);
                            highlightRoute(index, e);
                        });

                    }
                    map.fitBounds(bounds);
                }
            });
        }
        // this makes one of the colored routes visible.
        function highlightRoute(index, e) {
            for(var j in  polylines ) {
                if(j==index) {
                    //var color = '#0000ff';
                    polylines[j].setMap(map);
                    // feel free to customise this string
                    var contentString =
                        '<span>'+ data[j].distance.text +'</span><br/>'+
                        '<span>'+ data[j].duration.text +'</span><br/>'+
                        '<span>route: '+ j +'</span><br/>'+
                        //'From: <span>'+ data[j].start_address +'</span><br/>'+
                        //'To: <span>'+ data[j].end_address +'</span><br/>'+
                        '';
                    if(e) {
                       var position = new google.maps.LatLng(e.latLng.lat(), e.latLng.lng());
                        // it may be needed to close the previous infoWindow
                        if(infowindow) {
                            infowindow.close();
                            infowindow = null;
                        }
                        infowindow = new google.maps.InfoWindow({
                            content: contentString,
                            position: position,
                            map: map
                        });
                        //infowindow.open(map, polylines[j]);
                    }
                }
                else {
                    polylines[j].setMap(null);
                }
            }
        }
        // returns a polyline.
        // if hide is set to true, the line is not put on the map
        function drawPolyline(path, color, hide) {
            var line = new google.maps.Polyline({
                path: path,
                strokeColor: color,
                strokeOpacity: 0.9,
                strokeWeight: 3
            });
            if(! hide) {
                line.setMap(map);
            }
            return line;
        }
        function drawPolylineShadow(path, color, hide) {
            var line = new google.maps.Polyline({
                path: path,
                strokeColor: color,
                strokeOpacity: 0.4,
                strokeWeight: 7
            });
            if(! hide) {
                line.setMap(map);
            }
            return line;
        }
        google.maps.event.addDomListener(window, 'load', initMap);
    </script>
</body>
</html> 
<!DOCTYPE html>
<html>
<head>
    <title>Suggested routes</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
        html, body {
            height: 100%;
            margin: 0;
            padding: 0;
        }
        #map {
            height: 90%;
        }
    </style>
</head>
<body>
    <form id="form">
        <input id="from" placeholder="From" value="Brussel" />
        <input id="to" placeholder="To" value="Antwerpen" />
        <input type="submit" value="GO"  />
    </form>
    <div id="map"></div>
    <script type="text/javascript" src="https://maps.google.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry"></script>
    <script>
        var map;
        var directionsService;
        var polylines = [];

        function initMap() {
            map = new google.maps.Map(document.getElementById('map'), {
                center: {lat: 50.84659376378408, lng: 4.3531406857355215},
                zoom: 12,
                mapTypeId: 'terrain'
            });
            google.maps.event.addDomListener(document.getElementById('form'), 'submit', function(e) {
                calcRoute(
                    document.getElementById('from').value,
                    document.getElementById('to').value
                );
                // prevent the form from really submitting
                e.preventDefault();
                return false;
            });
            directionsService = new google.maps.DirectionsService();
            // get the bounds of the polyline
            // http://stackoverflow.com/questions/3284808/getting-the-bounds-of-a-polyine-in-google-maps-api-v3
            google.maps.Polyline.prototype.getBounds = function(startBounds) {
                if(startBounds) {
                    var bounds = startBounds;
                }
                else {
                    var bounds = new google.maps.LatLngBounds();
                }

                this.getPath().forEach(function(item, index) {
                    bounds.extend(new google.maps.LatLng(item.lat(), item.lng()));
                });
                return bounds;
            };
        }

        function calcRoute(start, end) {
            var request = {
                origin: start,
                destination: end,
                provideRouteAlternatives: true,
                unitSystem: google.maps.UnitSystem.METRIC,
                travelMode: google.maps.TravelMode['DRIVING']
            };
            directionsService.route(request, function(response, status) {
                // clear former polylines
                for(var j in  polylines ) {
                    polylines[j].setMap(null);
                }
                polylines = [];
                if (status == google.maps.DirectionsStatus.OK) {
                    var bounds = new google.maps.LatLngBounds();
                    // draw the lines in reverse orde, so the first one is on top (z-index)
                    for(var i=response.routes.length - 1; i>=0; i-- ) {
                        // let's make the first suggestion highlighted;
                        if(i==0) {
                            var color = '#0000ff';

                        }
                        else {
                            var color = '#999999';
                        }
                        var line = drawPolyline(response.routes[i].overview_path, color);
                        polylines.push(line);
                        bounds = line.getBounds(bounds);
                        google.maps.event.addListener(line, 'click', function() {
                            // detect which route was clicked on
                            var index = polylines.indexOf(this);
                            highlightRoute(index);
                        });
                    }
                    map.fitBounds(bounds);
                }
            });
        }

        function highlightRoute(index) {
            for(var j in  polylines ) {
                if(j==index) {
                    var color = '#0000ff';
                }
                else {
                    var color = '#999999';
                }
                polylines[j].setOptions({strokeColor: color});
            }
        }

        function drawPolyline(path, color) {
            var line = new google.maps.Polyline({
                path: path,
                strokeColor: color,
                strokeOpacity: 0.7,
                strokeWeight: 3
            });
            line.setMap(map);
            return line;
        }
        google.maps.event.addDomListener(window, 'load', initMap);
    </script>
</body>
</html>