2013-12-18 91 views
0

如何在同一张地图上显示两条路线?例如:A-> B y C-> D。 使用Google Maps API。如何在同一张地图上显示两条路线?

 function calcRoute1() { 
     var start = 'Huanchaco-Trujillo'; 
     var end = 'Lince-Lima'; 
     var request1 = { 
      origin:start, 
      destination:end, 
      travelMode: google.maps.TravelMode.DRIVING 
     }; 
     directionsService.route(request1, function(response1, status) { 
      if (status == google.maps.DirectionsStatus.OK) { 
      directionsDisplay.setDirections(response1); 
      } 
     }); 
     } 

     function calcRoute2() { 
     var start = 'Chiclayo-Lambayeque'; 
     var end = 'Tacna-Tacna'; 
     ... 
     } 
+0

是。使用两个不同的directionsRenderer实例(可能还有两个不同的directionsService实例)。如果两条路线的边界非常不同,则可能需要手动计算一个边界,以显示两者([google.maps.LatLngBounds.union](https://developers.google.com/maps/documentation/javascript/reference #LatLngBounds)) – geocodezip

+0

[类似问题](http://stackoverflow.com/questions/20366537/can-you-display-multiple-maps-on-the-same-page-with-directions-using-google-maps/ 20366832#20366832)(在不同的地图上有多组方向,但可以将它们放在同一张地图上) – geocodezip

回答

0

这个工作对我来说:

var directionsService1 = new google.maps.DirectionsService(); 
    var directionsDisplay1 = new google.maps.DirectionsRenderer(); 

    function calcRoute1() { 
     var start = 'Huanchaco-Trujillo'; 
     var end = 'Lince-Lima'; 
     var request1 = { 
      origin:start, 
      destination:end, 
      travelMode: google.maps.TravelMode.DRIVING 
     }; 
     directionsService1.route(request1, function(response1, status) { 
      if (status == google.maps.DirectionsStatus.OK) { 
      directionsDisplay1.setDirections(response1); 
     directionsDisplay1.setMap(map); 
      } 
     }); 
     } 

     var directionsService2 = new google.maps.DirectionsService(); 
     var directionsDisplay2 = new google.maps.DirectionsRenderer(); 
     function calcRoute2() { 
     var start = 'Chiclayo-Lambayeque'; 
     var end = 'Tacna-Tacna'; 
     var request2 = { 
      origin:start, 
      destination:end, 
      travelMode: google.maps.TravelMode.DRIVING 
     }; 
     directionsService2.route(request2, function(response2, status) { 
      if (status == google.maps.DirectionsStatus.OK) { 
      directionsDisplay2.setDirections(response2); 
     directionsDisplay2.setMap(map); 
      } 
     }); 
     } 
google.maps.event.addDomListener(window, 'load', load); 

working example

相关问题