谷歌地图 - 显示相同路线的不同运输方式

时间:2013-11-27 16:44:25

标签: google-maps

我正在探索Google地图API v3。 有没有办法同时显示从A点到B点的不同方式? 公共汽车,步行车?

我做了类似的事情,但很明显,它覆盖了我写的第一部。 有什么帮助吗?

    function calcRoute() {
            //            var start = document.getElementById('start').value;
            //            var end = document.getElementById('end').value;
            var request = {
                origin: "Avenida da Liberdade, Lisboa",
                destination: "Campo Grande, Lisboa",
                travelMode: google.maps.TravelMode.TRANSIT
            };
            directionsService.route(request, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setDirections(response);
                    directionsDisplay.setMap(map);
                }
            });


            var request2 = {
                origin: "Avenida da Liberdade, Lisboa",
                destination: "Campo Grande, Lisboa",
                travelMode: google.maps.TravelMode.DRIVING
            };
            directionsService.route(request2, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setDirections(response);
                    directionsDisplay.setMap(map);
                }
            });

        }

<input type="button" title="Transportes"  onclick="calcRoute();"/>

1 个答案:

答案 0 :(得分:0)

使用路线服务和路线渲染器的不同实例:

    function calcRoute() {
        //            var start = document.getElementById('start').value;
        //            var end = document.getElementById('end').value;
        var request = {
            origin: "Avenida da Liberdade, Lisboa",
            destination: "Campo Grande, Lisboa",
            travelMode: google.maps.TravelMode.TRANSIT
        };
        directionsServiceTransit.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplayTransit.setDirections(response);
                directionsDisplayTransit.setMap(map);
            }
        });


        var request2 = {
            origin: "Avenida da Liberdade, Lisboa",
            destination: "Campo Grande, Lisboa",
            travelMode: google.maps.TravelMode.DRIVING
        };
        directionsServiceDriving.route(request2, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplayDriving.setDirections(response);
                directionsDisplayDriving.setMap(map);
            }
        });

    }

<input type="button" title="Transportes"  onclick="calcRoute();"/>