Displaying Multiple Routes using Google Maps Api

<!–Source code –>

<!-- Style to put some height on the map -->
<style type="text/css">
    #map-canvas { height: 500px };

<!-- Load the Google Maps aPI -->
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDPk4QYh_uNfP25-JYBBN8lU-nd85XhR0A&sensor=false"></script>

<!-- All of the script for multiple requests -->
<script type="text/javascript">

    // Initialise some variables
    var directionsService = new google.maps.DirectionsService();
    var num, map, data;
    var requestArray = [], renderArray = [];

    // A JSON Array containing some people/routes and the destinations/stops
    var jsonArray = {
        "Person 1": ["Torquay", "Teignmouth", "Dawlish", "Exeter"],
        "Person 2": ["Exmouth", "Sidmouth", "Taunton", "Crediton", "Okehampton"],
        "Person 3": ["Penzance", "Truro", "Bodmin", "Falmouth"]

    // 16 Standard Colours for navigation polylines
    var colourArray = ['navy', 'grey', 'fuchsia', 'black', 'white', 'lime', 'maroon', 'purple', 'aqua', 'red', 'green', 'silver', 'olive', 'blue', 'yellow', 'teal'];

    // Let's make an array of requests which will become individual polylines on the map.
    function generateRequests(){

        requestArray = [];

        for (var route in jsonArray){
            // This now deals with one of the people / routes

            // Somewhere to store the wayoints
            var waypts = [];

            // 'start' and 'finish' will be the routes origin and destination
            var start, finish

            // lastpoint is used to ensure that duplicate waypoints are stripped
            var lastpoint

            data = jsonArray[route]

            limit = data.length
            for (var waypoint = 0; waypoint < limit; waypoint++) {
                if (data[waypoint] === lastpoint){
                    // Duplicate of of the last waypoint - don't bother

                // Prepare the lastpoint for the next loop
                lastpoint = data[waypoint]

                // Add this to waypoint to the array for making the request
                    location: data[waypoint],
                    stopover: true

            // Grab the first waypoint for the 'start' location
            start = (waypts.shift()).location;
            // Grab the last waypoint for use as a 'finish' location
            finish = waypts.pop();
            if(finish === undefined){
                // Unless there was no finish location for some reason?
                finish = start;
            } else {
                finish = finish.location;

            // Let's create the Google Maps request object
            var request = {
                origin: start,
                destination: finish,
                waypoints: waypts,
                travelMode: google.maps.TravelMode.DRIVING

            // and save it in our requestArray
            requestArray.push({"route": route, "request": request});


    function processRequests(){

        // Counter to track request submission and process one at a time;
        var i = 0;

        // Used to submit the request 'i'
        function submitRequest(){
            directionsService.route(requestArray[i].request, directionResults);

        // Used as callback for the above request for current 'i'
        function directionResults(result, status) {
            if (status == google.maps.DirectionsStatus.OK) {

                // Create a unique DirectionsRenderer 'i'
                renderArray[i] = new google.maps.DirectionsRenderer();

                // Some unique options from the colorArray so we can see the routes
                    preserveViewport: true,
                    suppressInfoWindows: true,
                    polylineOptions: {
                        strokeWeight: 4,
                        strokeOpacity: 0.8,
                        strokeColor: colourArray[i]
                            path: google.maps.SymbolPath.BACKWARD_CLOSED_ARROW,
                            scale: 3,
                            strokeColor: colourArray[i]

                // Use this new renderer with the result
                // and start the next request


        function nextRequest(){
            // Increase the counter
            // Make sure we are still waiting for a request
            if(i >= requestArray.length){
                // No more to do
            // Submit another request

        // This request is just to kick start the whole process

    // Called Onload
    function init() {

        // Some basic map setup (from the API docs)
        var mapOptions = {
            center: new google.maps.LatLng(50.677965, -3.768841),
            zoom: 8,
            mapTypeControl: false,
            streetViewControl: false,
            mapTypeId: google.maps.MapTypeId.ROADMAP

        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        // Start the request making

    // Get the ball rolling and trigger our init() on 'load'
    google.maps.event.addDomListener(window, 'load', init);

<!-- Somewhere in the DOM for the map to be rendered -->
<div id="map-canvas"></div>