title: find associated polylines for selected marker/circlemarker/point in leaflet
body: <p>I have a multiple circlemarkers on map.</p>
  
  <p>I want to find how many polylines passes through that marker &amp; want to remove that polyline &amp; If polyline does not exists then I want to add polyline.</p>
  
  <p>I am using leaflet. </p>
  
  <pre><code>          &lt;script type="text/javascript"&gt;
  
        function init(){
      var map = L.map('map').setView([51.49521, -0.10062], 13);
      L.tileLayer('http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png', {
          maxZoom: 18,
          attribution: 'Map data &amp;copy; &lt;a href="http://openstreetmap.org"&gt;OpenStreetMap&lt;/a&gt; contributors, &lt;a href="http://creativecommons.org/licenses/by-sa/2.0/"&gt;CC-BY-SA&lt;/a&gt;, Imagery © &lt;a href="http://cloudmade.com"&gt;CloudMade&lt;/a&gt;'
      }).addTo(map);
  
      // get all 6 points
      var points = [
          [51.49346, -0.11518],
          [51.49827, -0.06763],
          [51.48331, -0.08154],
          [51.52284, -0.09974],
          [51.51932, -0.06695],
          [51.50949, -0.1363]
      ];
  
      // polyline
      var selection = [];
      var polyline = new L.Polyline([], {
          color: 'blue',
          weight: 5,
          smoothFactor: 1
      }).addTo(map);
  
      var changeMarkerState = function (marker, select) {
          if (marker instanceof L.CircleMarker) {
              if (select) {
                  marker.setRadius(25);
              } else {
                  marker.setRadius(10);
              }
          }
          if (marker instanceof L.Marker) {
              if (select) {
                  marker.options.title = 'selected';
              } else {
                  marker.options.title = 'unselected';
              }
              marker.setIcon(new L.Icon.Default());
          }
      };
  
      var onClick = function () {
          var index = selection.indexOf(this);
          if (index !== -1) {
              changeMarkerState(this, false);
              selection.splice(index, 1);
              polyline.spliceLatLngs(index, 1);
          } else {
              changeMarkerState(this, true);
              selection.push(this);
              polyline.addLatLng(this.getLatLng())
          }
      };
  
      // centerpoint
      var centerPoint = new L.LatLng(51.49521, -0.10062);
      var marker1 = L.marker([51.49521, -0.10062],
              {title: 'unselected'}).on('click', onClick).addTo(map);
  
  
      // adding allo points to map
      for (var i = 0, l = points.length; i &lt; l; i++)
      {
          // here I can use marker also(if solution is possible with markers)
          L.circleMarker(points[i]).on('click', onClick).addTo(map);
          var myPoint = new L.LatLng(points[i][0],points[i][1]);
          var myPointList = [myPoint, centerPoint];
  
      var firstpolyline = new L.Polyline(myPointList, {
      color: 'red',
      weight: 5,
      smoothFactor: 1
  
      }).addTo(map);
      }
  
      }
        &lt;/script&gt;
  </code></pre>
  
  <p>In above code what I am doing is that I am drawing multiple red polylines from different circlemarkers to one center point.</p>
  
  <p>On selection of two circle markers I am drawing blue polyline between them.</p>
  
  <p>At same time I want to remove the red polyline which there between circlemarkers &amp; centerpoint.</p>
  
  <p>Also If circlemarker is unselected then that red polyline between that circlemarker &amp; centerpoint should be added.</p>