title: Clear Marker Layers (Leaflet )
body: <p>I am using Leaflet library and stuck with following issue:
  To generate Map i am calling map function on button click. 
  So on each generatemap function call, I want to clear pregenerated markers.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function generatefilterrecord(orgid,defservice,defavail,defowner,defhealthfacility) {
  
      $("#content1").hide();
      $("#map1").show();
  
      $("#mapA1").hide();
      $("#footer").hide();
      jQuery('#sel').html('');
      $(".w3-card-4").remove();
  
  
      var addressjoin=[],ouid=[],availspecialitiesjoin,availspecialities=[],avaialabilityjoin,name=[],address=[],pincode=[],village=[],mobile=[],special=[],notspecial=[],hfacilities=[],nothfacilities=[],schemes=[],notschemes=[],contactpname=[],contactpnumber=[];
      var toAdd,spec=[],email=[],specialjoin,notspecialjoin,notspec=[],owner=[],notowner=[],hfacilitiesjoin,nothfacilitiesjoin,schemesjoin,notschemesjoin,hfschemes=[],nothfschemes=[];
      var healthfac="",ownership=[],availspecialiti=[];
      var arrayMap = [];latitude=[];longitude=[];
  
  
      $.getJSON("../../api/analytics/events/query/tzR46QRZ6FJ.json?stage=o6ps51YxGNb&amp;dimension=pe:THIS_YEAR&amp;dimension=ou:"+orgid+"&amp;dimension=l8VDWUvIHmv&amp;dimension=KOhqEw0UKxA&amp;dimension=xjJR4dTmn4p&amp;dimension=wcmHow1kcBi&amp;dimension=pqVIj8NyTXb&amp;dimension=g7vyRbNim1K&amp;dimension=Gx4VSNet1dC&amp;dimension=bUg8a8bAvJs&amp;dimension="+defservice+"&amp;dimension="+defavail+"&amp;dimension="+defowner+"&amp;dimension="+defhealthfacility+"&amp;dimension=ZUbPsfW6y0C&amp;dimension=CAOM6riDtfU&amp;dimension=YL7OJoQCAmF&amp;dimension=vJO1Jac84Ar&amp;dimension=kF8ZJYe9SJZ&amp;dimension=tNhLX6c7KHp&amp;dimension=bVENUe0eDsO&amp;displayProperty=NAME", function (data) {
          console.log("../../api/analytics/events/query/tzR46QRZ6FJ.json?stage=o6ps51YxGNb&amp;dimension=pe:THIS_YEAR&amp;dimension=ou:"+orgid+"&amp;dimension=l8VDWUvIHmv&amp;dimension=KOhqEw0UKxA&amp;dimension=xjJR4dTmn4p&amp;dimension=wcmHow1kcBi&amp;dimension=pqVIj8NyTXb&amp;dimension=g7vyRbNim1K&amp;dimension=Gx4VSNet1dC&amp;dimension=bUg8a8bAvJs&amp;dimension="+defservice+"&amp;dimension="+defowner+"&amp;dimension="+defhealthfacility+"&amp;dimension=jXCd8k2841l&amp;dimension=RkP5neDLbHv&amp;dimension=avHST8wLPnX&amp;dimension=txl9e6UJFP4&amp;dimension=ZUbPsfW6y0C&amp;dimension=CAOM6riDtfU&amp;dimension=YL7OJoQCAmF&amp;dimension=vJO1Jac84Ar&amp;dimension=kF8ZJYe9SJZ&amp;dimension=tNhLX6c7KHp&amp;dimension=bVENUe0eDsO&amp;displayProperty=NAME");
          var constants={key:name, value: value}
  
          analyticsMap = calculateIndex(data.headers,analyticsMap);
  
          if(data.rows.length==0)
          {
              alert("No result found for above selection");
          }
          for(var k=0;k&lt;data.rows.length;k++){
  
              arrayMap["special"] = special;
              arrayMap["name"] = name;
              arrayMap["address"] = addressjoin;
              arrayMap["pincode"] = pincode;
              arrayMap["village"] = village;
              arrayMap["mobile"] = mobile;
              arrayMap["notspecial"] = notspecial;
              arrayMap["hfacilities"] = hfacilities;
              arrayMap["nothfacilities"] = nothfacilities;
              arrayMap["schemes"] = schemes;
              arrayMap["notschemes"] = notschemes;
              arrayMap["contactpname"] = contactpname;
              arrayMap["contactpnumber"] = contactpnumber;
              arrayMap["availspecialities"] = availspecialities;
              arrayMap["ownership"] = ownership;
              arrayMap["ouid"] = ouid;
  
              for (var j=0;j&lt;analyticsMap.length;j++){
  
                  if (analyticsMap[j].index &gt; 0){
                      var value = data.rows[k][analyticsMap[j].index];
                      if (value == "1"){
                          value = data.headers[analyticsMap[j].index].column;
                      }
  
                      if (!value || value == "0"){
                          value = "";
                      }
                      if(arrayMap[analyticsMap[j].arrayName]){
                          arrayMap[analyticsMap[j].arrayName].push(value);
                      }
                  }
              }
              specialjoin = myJoin(special);
              availspecialitiesjoin = myJoin(availspecialities);
              notspecialjoin = myJoin(notspecial);
              hfacilitiesjoin = myJoin(hfacilities);
              nothfacilitiesjoin = myJoin(nothfacilities);
              schemesjoin = myJoin(schemes);
              notschemesjoin = myJoin(notschemes);
  
              spec.push(specialjoin);
              notspec.push(notspecialjoin);
              owner.push(hfacilitiesjoin);
              availspecialiti.push(availspecialitiesjoin);
              notowner.push(nothfacilitiesjoin);
              hfschemes.push(schemesjoin);
              nothfschemes.push(notschemesjoin);
  
  
              availspecialities=[];
              special = [];
              notspecial = [];
              hfacilities = [];
              nothfacilities = [];
              schemes = [];
              notschemes = [];
  
  
  
  
          }
          var header = {
              "Authorization": "Basic " + btoa( "homepage" + ':' + "Homepage123@123" )
          };
          for (var i = 0; i &lt; name.length; i++) {
              $.ajax({
                  async: false,
                  type: "GET",
                  dataType: "json",
                  contentType: "application/json",
                  header: header,
                  url: '../../api/organisationUnits/' + ouid[i] + '.json?fields=[id,name,coordinates]',
                  success: function (response) {
                      var coordinates = JSON.parse(response.coordinates);
                      latitude.push(coordinates[0]);
                      longitude.push(coordinates[1]);
  
  
                  },
                  error: function (response) {
  
                  }
              });
          }
  
          for (var i = 0; i &lt; name.length; i++) {
              if(ownership[i]=="Public")
              {
                  L.marker([longitude[i], latitude[i]], {icon: blueMarker}).addTo(map1).bindPopup(name[i]+","+"&lt;/br&gt;&lt;strong&gt;Contact:&lt;/strong&gt; "+  mobile[i]+ ",&lt;/br&gt; &lt;strong&gt;Schemes:&lt;/strong&gt;"+hfschemes[i]+", &lt;/br&gt;&lt;strong&gt;Availabilities:&lt;/strong&gt; "+availspecialiti[i]).openPopup();
              }
              else if(ownership[i]=="Private")
              {
                  L.marker([longitude[i], latitude[i]], {icon: redMarker}).addTo(map1).bindPopup(name[i]).openPopup();
              }
  
  
          }
  
      });
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here map never generates but if i remove marker.clearLayers(); then it is coming fine but problem is it appends every result so I want to clear the last generated markers.</p>