Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying multiple routes on a google map

Tags:

google-maps

I am trying to display multiple routes on same map but am unable to do so.

No matter what I do I get only one route.

function calcRoute() {
        var start = document.getElementById('start').value;
        var end = document.getElementById('end').value;
        var request = {
          origin: start,
          destination: end,
          travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
        directionsService.route(request, function(response, status) {
          if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
          }
        });
      }

Any pointers will be helpful.

like image 301
AJ. Avatar asked Jan 28 '12 05:01

AJ.


1 Answers

I had the same problem. This thread on a Google group for Google maps shows how to do it.

The author (a Google employee), wrote this:

You should be able to create two DirectionsRenderer objects, each that use the same map and different DirectionsResults.

var map = new google.maps.Map(document.getElementById("map_canvas"));
function renderDirections(result) {
  var directionsRenderer = new google.maps.DirectionsRenderer;
  directionsRenderer.setMap(map);
  directionsRenderer.setDirections(result);
}

var directionsService = new google.maps.DirectionsService;
function requestDirections(start, end) {
  directionsService.route({
    origin: start,
    destination: end,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
  }, function(result) {
    renderDirections(result);
  });
}
requestDirections('Huntsville, AL', 'Boston, MA');
requestDirections('Bakersfield, CA', 'Vancouver, BC');

I tried it and it does work.

like image 108
Joshua Frank Avatar answered Oct 07 '22 12:10

Joshua Frank