2013-02-15 100 views
4

我正在與Open Street Maps和使用leaflet library。 我有一個圖層組是這樣的:沿着它的圓圈刪除標記

var testLayer = new L.LayerGroup(); --> declared in a different file 

我想標記添加到標記組,添加圍繞每個標記一個圓圈,並標記組添加到地圖:

enter image description here

var realMarker = L.marker([52.6432, -6.53412 ],{icon: Icon,title:data[choice][key]["name"] 
         }).bindPopup("Dublin").addTo(testLayer); 
var circle = L.circle([52.6432, -6.53412 ],400, { 
         color: 'red', 
         fillColor: '#f03', 
         fillOpacity: 0.5 
        }).addTo(testLayer); 

map.addLayer(testLayer); 

但是,當我通過使用removeLayer()從標記組中刪除特定標記時,標記將從地圖中刪除,但該圓被留下。我怎樣才能將標記與圓圈一起刪除?

回答

6

我會存儲在標記圓一個參考:

var realMarker = L.marker([52.6432, -6.53412 ], 
          {icon: Icon,title:data[choice][key]["name"]} 
         ).bindPopup("Dublin").addTo(testLayer); 
var circle = L.circle([52.6432, -6.53412 ],400, { 
         color: 'red', 
         fillColor: '#f03', 
         fillOpacity: 0.5 
        }).addTo(testLayer); 
realMarker.circle=circle; 
map.addLayer(testLayer); 

然後,在刪除標記時,也刪除了一圈

testLayer.removeLayer(markerToRemove); 
testLayer.removeLayer(markerToRemove.circle); 
+0

+1沒想到以這種方式....偉大它做了魔術......乾杯...... – troy 2013-02-15 16:33:50