title: Uncaught TypeError: Cannot read property 'route'
body: <p>I used Google Maps API to build a map that will plot the points based on their latitude and longitude from 'whereNew.js' with markers whose color will be dependent on another parameter from 'MarketData.js' . When the marker is clicked, it'll display an infowindow with a button-"Show Routes" and some informations based on 'MarketData.js'. The code executed successfully upto this point.</p>
  
  <p>After this, when the "Show Routes" button from any marker is clicked, I want Google Maps API to show all the routes from every location of the 8th column of 'MarketData.js" to that marker. However, when I click on that button, it is giving this error : </p>
  
  <blockquote>
    <p>Uncaught TypeError: Cannot read property 'route' of undefined at calculateAndDisplayRoute </p>
    
    <p>whereNewTest1.html:162</p>
  </blockquote>
  
  <p>I don't understand what I'm doing wrong. Please Help</p>
  
  <p><strong>whereNewTest1.html</strong></p>
  
  <pre><code>    &lt;html&gt;
      &lt;meta name="viewport" content="initial-scale=1.0, user-scalable=no"&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;title&gt;Markets of Kolkata&lt;/title&gt;
      &lt;style&gt;
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
  
      html,
      body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
  
      #floating-panel {
        position: absolute;
        top: 10px;
        left: 25%;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
        text-align: center;
        font-family: 'Roboto', 'sans-serif';
        line-height: 30px;
        padding-left: 10px;
      }
  
      #warnings-panel {
        width: 100%;
        height: 10%;
        text-align: center;
      }
      &lt;/style&gt;
      &lt;link href="http://google-developers.appspot.com/maps/documentation/javascript/examples/default.css" rel="stylesheet"&gt;
  
      &lt;!-- If you are in China, you may need to use theis site for the Google Maps code
      &lt;script src="http://maps.google.cn/maps/api/js" type="text/javascript"&gt;&lt;/script&gt; --&gt;
      &lt;!--&lt;script src="http://maps.googleapis.com/maps/api/js?sensor=false"&gt;&lt;/script&gt; --&gt;
      &lt;!-- &lt;script async defer src=
          "https://maps.googleapis.com/maps/api/js?libraries=geometry,drawing&amp;key=AIzaSyDrdSH53E0MRIfajUGZQiFHAB0aLfzFIVU&amp;v=3&amp;v=3&amp;callback=displayDirections"&gt;&lt;/script&gt;
      &lt;/script&gt; --&gt;
    &lt;script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBbXta5ppMyzPmTCGNsyP-djMlSOGJ9t9o&amp;callback=initialize"&gt;
    &lt;/script&gt;
  
      &lt;script src="whereNew.js"&gt;&lt;/script&gt;
      &lt;script src="marketData.js"&gt;&lt;/script&gt;
      &lt;script&gt;
  
      var directionsDisplay;
      var directionsService = new google.maps.DirectionsService();
      var map;
      var markerArray = [];
      var dest ;
      function initialize() 
      {
          //alert("To see the title of a marker, hover over the marker but don't click.");
          var myLatlng = new google.maps.LatLng(22.39361,88.099263)
          var mapOptions = {
              zoom: 3,
              center: myLatlng,
              mapTypeId: google.maps.MapTypeId.ROADMAP
              }
  
          var markerArray = [];
          var directionsService = new google.maps.DirectionsService;
          var directionsDisplay = new google.maps.DirectionsRenderer(
          {
              map: map
          });
  
  
          var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
          i = 0;
          var markers = [];
  
          for ( pos in myData ) 
          {    
              var row = myData[pos];
              var row1 = MarketInfo[i];
              window.console &amp;&amp; console.log(row) &amp;&amp; console.log(row1);
              // if ( i &lt; 3 ) { alert(row); }
              var newLatlng = new google.maps.LatLng(row[0], row[1]);
              var marker = new google.maps.Marker(
              {
                  position: newLatlng,
                  map: map,
                  title: row[3],
              });
  
              var infowindow = new google.maps.InfoWindow({ });
              var largeInfowindow = new google.maps.InfoWindow({ });
              dest = row1[1];
              var content = row1[2] + '&lt;br/&gt;&lt;button onclick = "calculateAndDisplayRoute(directionsDisplay, directionsService, markerArray, map, dest);"&gt;Show Routes&lt;/button&gt;';
  
              if((content.search("WHOLESALE"))&gt;-1)
              {
                  marker.setIcon('http://maps.google.com/mapfiles/ms/icons/red-dot.png');
              }
              else if((content.search("WHOLE SALE"))&gt;-1)
              {
                  marker.setIcon('http://maps.google.com/mapfiles/ms/icons/red-dot.png');
              }
              else if((content.search("MAJOR"))&gt;-1)
              {
                  marker.setIcon('http://maps.google.com/mapfiles/ms/icons/blue-dot.png');
              }
              else
              {
                  marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png');
              }
  
              google.maps.event.addListener(marker,'click', (function(marker,content,infowindow)
              { 
                  return function() 
                  {
                      infowindow.setContent(content);
                      infowindow.open(map,marker);
                  };
              })(marker,content,infowindow));
  
              markers.push(marker);   
              i = i + 1;
          }
  
  
  
  
          function populateInfoWindow(marker, infowindow)
          {
              if(infowindow.marker != marker)
              {
                  infowindow.marker = marker;
                  infowindow.setContent('&lt;div&gt;'+infowindow.content+'&lt;/div&gt;');
                  infowindow.open(map, marker);
  
                  infowindow.addListener('closeclick', function(){
                  infowindow.setMarker(null);
                  });
  
              }   
          } 
      }
  
      function calculateAndDisplayRoute(directionsDisplay, directionsService, markerArray, map, dest) 
      {
          for (var i = 0; i &lt; markerArray.length; i++) 
          {
          markerArray[i].setMap(null);
          }
  
          for (pos in MarketInfo) 
          {
          var row = MarketInfo[pos];
          // Second loop for multiple destination
          var str_array = row[7].split(',');
          for (var j = 0; j &lt; str_array.length; j++) 
          {
              directionsService.route(
              {
              origin: str_array[j],
              destination: dest,
              travelMode: 'DRIVING'
              }, function(response, status) {
                  if (status === 'OK') 
                  {
                      document.getElementById('warnings-panel').innerHTML = '&lt;b&gt;' + response.routes[0].warnings + '&lt;/b&gt;';
                      var dirDisplay = new google.maps.DirectionsRenderer({
                      map: map
                  });
                  dirDisplay.setDirections(response);
                  }else 
                  {
                      window.alert('Directions request failed due to ' + status);
                  }
            });
          }
      }
      }
  
  
      &lt;/script&gt;
  
    &lt;body onload="initialize()"&gt;
  &lt;div id="map_canvas" style="height: 600px"&gt;&lt;/div&gt;
  &lt;p&gt;
  &lt;b&gt;Developed by&lt;/b&gt;
  &lt;a href="http://www.linkedin.com/in/arnab-chakravarty-8a329111b?trk=nav_responsive_tab_profile"&gt;&lt;b&gt;Arnab Chakravarty&lt;/b&gt;&lt;/a&gt;.
  &lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>whereNew.js</strong></p>
  
  <pre><code>myData = [ 
  [22.5193768,88.3656851, 'K.M.C. Market, 3rd Floor Market Complex, 212, Rash Behari Avenue Road, Hindustan Park, Gariahat, Kolkata, West Bengal 700019, India', 'PAL BAZAR'], 
  [22.4977887,88.3796042, 'Garfa Main Rd, Kolkata, West Bengal 700075, India', 'RAMLAL BAZAR'], 
  [22.5048463,88.3882325, 'Purbachal Main Rd, Ramlal Bazar, Haltu, Kolkata, West Bengal 700078, India', 'COLLEGE STREET MARKET ( SPORTS GOODS)'] 
  ];
  </code></pre>
  
  <p><strong>MarketData.js</strong></p>
  
  <pre><code>MarketInfo = [
  ["GARIAHAT KMC MARKET", "K.M.C. Market, 3rd Floor Market Complex, 212, Rash Behari Avenue Road, Hindustan Park, Gariahat, Kolkata- 700019", "MAJOR (1400SHOP + HOWKER)", "FRUITS, VEGETABLES, FLOWER, FISH, MEAT, GROCERY, CLOTHING, FURNITURE , HOUSEHOLD, FOOTWEAR ELECTRICAL ETC", "", "", "BARABAZAR, KOLEYMART", "HOWRAH", "We collect the goods ourselves", "", "MANY", "Cars, Light goods vehicles (LGV), Heavy goods vehicles/lorries (HGV)", "Within one hour", "", "", "No problems encountered", "", "IF A UNLOADING SPACE CAN PROVIDE BY GOVT. IT WILL BE BETTER"], 
  ["PAL BAZAR", "GARIFA MAIN ROAD, KOLKATA - 700 075", "MINOR (150SHOP + 70HOWKERS)", "FRESH, COOL AND FROZEN PRODUCT, OTHER TYPE OF CONSUMER GOODS, TEXTILE,FOOTWEAR, ELECTRONIC AND ELECTRICAL EQUIPMENT, BUILDING MATERIALS", "", "SANDHYA BAZAR", "BARA BAZAR, CHADNI CHOWK", "", "The goods are delivered by multiple suppliers", "MANY", "10 TO 20", "Bicycles/tricycles/carts, Motorised two/three-wheelers", "Within one hour", "", "", "Delivery vehicle causes traffic jams in front of the establishment", "", ""], 
  ["RAMLAL BAZAR", "Purbachal Main Rd, Ramlal Bazar, Haltu. Kolkata-700078", "MINOR (150 SHOP+ 180 HOWKERS)", "FRUITS, VEGETABLE, FISH, MEAT, GROCERY, TEXTILE, ELECTRICAL, BUILDING MATERIALS ETC", "", "BAGHAJATIN AND SANDHYA BAZAR", "BARA BAZAR, EZRA ST.", "", "We collect the goods ourselves", "", "MANY", "Bicycles/tricycles/carts, Motorised two/three-wheelers, Light goods vehicles (LGV)", "Within one hour", "", "", "Limited storage space available, Delivery vehicle causes traffic jams in front of the establishment, High delivery costs", "", "PARKING PLACE REQUIRE"]
  ];
  </code></pre>