title: google maps api v3 draw polyline between draggable markers
body: <p>I have multiple markers on google map v3. When map gets loaded it draws all marker and polyline. Initial path of the polyline would be same LatLng of each marker. When you drag marker it should draw the line between original LatLng to new position of the marker. You can move all and/or any markers on the map and each marker should have separate line showing marker's original LatLng to new position. I have done similar thing in below code but the problem is when I move marker it looses previous line of the marker. It always shows only one line of the dragged marker. How to solve this problem. Thanks in advance... Ash</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Marker with line example two&lt;/title&gt;
      &lt;style type="text/css"&gt;
          html {
              height: 100%;
          }
  
          body {
              height: 100%;
              margin: 0;
              padding: 0;
          }
  
          #map_canvas {
              height: 100%;
          }
      &lt;/style&gt;
      &lt;script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="Scripts/jquery-1.10.2.min.js"&gt;&lt;/script&gt;
      &lt;script&gt;
          var line;
          var myLatlng = new google.maps.LatLng(41.7833, 5.2167);
          var marker;
          var lines = [];
          function initialize() {
              var domain = [new google.maps.LatLng(11.2583, 75.1374)];
              var markers = [];
  
              var mapOptions = {
                  zoom: 2,
                  center: myLatlng,
                  mapTypeId: google.maps.MapTypeId.ROADMAP,
                  opacity: 0.2,
                  disableDefaultUI: true,
                  draggable: false
              };
  
              map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
  
              var lineCoordinates = [
                new google.maps.LatLng(53.215556, 56.949219),
                new google.maps.LatLng(75.797201, 125.003906),
                new google.maps.LatLng(37.7833, 144.9667),
                new google.maps.LatLng(-24.797201, 26.003906),
                new google.maps.LatLng(27.797201, -101.003906)
              ];
  
              for (i = 0; i &lt; lineCoordinates.length; i++) {
                  var latLng = lineCoordinates[i],
                      marker = new google.maps.Marker({
                          position: latLng,
                          draggable: true,
                          title: i.toString(),
                          map: map,
                      });
                  markers.push({
                      key: i.toString(),
                      latLng: latLng
                  });
  
                  line = new google.maps.Polyline({
                      path: [latLng, latLng],
                      strokeOpacity: 0.8,
                      strokeWeight: 2,
                      strokeColor: '#f00',
                      geodesic: true                    
                  });
                  line.setMap(map);
  
                  lines.push(line);
  
                  google.maps.event.addListener(marker, 'drag', function (event) {
                      var title = this.title,
                          result = $.grep(markers, function (e) { return e.key === title }),
                          oldLatLng = result[0].latLng,
                          newLatLng = new google.maps.LatLng(this.getPosition().lat(), this.getPosition().lng());
  
                      line.setPath([oldLatLng, newLatLng]);
                  });
  
              } //end of for loop
          } //end of initialize function
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body onload="initialize()"&gt;
      &lt;div id="map_canvas" style="width: 1000px; height: 675px; margin-left: 400px; margin-top: 38px;"&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>