2013-11-27 68 views
0

我探索谷歌地圖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();"/> 
+0

相關問題已經回答[這裏](http://stackoverflow.com/a/5498431/1921385) – Moob

回答

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();"/>