2017-12-01 8 views
1

여기에 내 PHP를 기반으로지도에 마커를 표시하는 코드가 있습니다.레이어 제거가 작동하지 않습니다.

/* Generate the map and center it in philipines on start up */ 
var map = L.map('map').setView([12.8797, 121.7740], 6); 
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', { 
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' 
}).addTo(map); 

/* Mapping Variables */ 
var markers = new L.layerGroup(); 

/* On button click put a marker on the phillipine map base on condition */ 
$("#btn-generate").click(function(){ 

/* Other variables */ 
var d1 = $('#startdate').val(); 
var d2 = $('#enddate').val(); 
var fspcode = $('#fspcode').find(":selected").text(); 

$.ajax({ 
    type: 'POST', 
    url: '../../php/pages/sfa/qry_map.php', 
    data: { 
    'startdate' : d1, 
    'enddate' : d2, 
    'fspcode' : fspcode 
    }, 
    success: function(data) { 
     var data = JSON.parse(data); 
     data = data.aaData; 
     if (data.length == 0) { 
      sfaMsgbox("Unable to Track. No record found."); 
     } else { 
      /* Get the lat and long */ 
      for (var i = 0; i < data.length; i++) { 
      var lat = data[i].lat; 
      var long = data[i].long; 
      var code = data[i].code; 
      var vdate = data[i].vdate; 
      var type = data[i].type; 
      var name = data[i].name; 
      var address = data[i].address; 
      var wholeinfo = "<b>FSP Assigned : </b>" + code + "<br>" + 
       "<b>Visit Date : </b>" + vdate + "<br>" + 
       "<b>Customer : </b>" + name + "<br>" + 
       "<b>Address : </b>" + address + "<br>" + 
       "<b>Type : </b>" + type; 

      /* Proceed in creating the map */ 
      var marker = L.marker([lat,long]); 
      marker.bindPopup(wholeinfo, { 
       showOnMouseOver: true 
      }); 
      markers.addLayer(marker); 
     } 
    } 
} 
}); 

/* Add the markers */ 
map.addLayer(markers); 

}); 

그리고 삭제 방법입니다.

그러나 모든 마커를 삭제하면 삭제되지만 삭제하면 새 마커가 다시 생성됩니다. 어떻게 해결할 수 있습니까?

여기 내 목표는 마커를 몇 개 추가 하든지 추가하고 모든 마커를 삭제하는 것입니다. 유일한 문제는 이전 마커가 다시 표시된다는 것입니다. TYSM

답변

2

removeLayer 함수는지도에서 레이어 만 제거합니다 (기본적으로 숨 깁니다). 마커는 여전히 layerGroup에 남아 있습니다. 당신이 레이어에서 모든 마커를 제거하려는 경우, 당신은 아마 대신

markers.clearLayers(); 

를 사용하고 싶습니다. 이렇게하면 레이어의 모든 현재 마커가 제거됩니다.

+0

우수 답변 TYSM –