无法删除以编程方式添加的L.circle或L.polygon,但L.markers很好

时间:2014-06-09 19:31:56

标签: leaflet

我尝试在页面刷新期间保留绘制的项目,但遇到了圆圈,多边形和标记的问题。标记工作正常但圆形和多边形不可移除:

// basic map setup taken from tutorial
var drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
var drawControl = new L.Control.Draw({
  edit: {
    featureGroup: drawnItems
  },
  draw: {
    polyline: false
  }
});
map.addControl(drawControl);

// this works fine and the markers are able to be deleted by the delete control
var pointMarker = new L.marker(new L.LatLng(pointLat, pointLon));
pointMarker.addTo(drawnItems);

// this will draw the circle/polygon
// it is not selectable for deletion using the remove toolbar
// other circles/polygons created using the draw toolbar are removable
var circle = new L.circle(new L.LatLng(circleLat, circleLon), circleRadius);
circle.addTo(drawnItems);

另一方面:编辑工具栏按钮允许编辑圆/多边形,因此删除和编辑功能之间显然存在一些差异。

1 个答案:

答案 0 :(得分:0)

尝试将创建的项目直接添加到draw:created事件中的drawnItems。这就是我正在做的事情,我可以添加和删除所有类型。

map.on('draw:created', function (e) {
            var type = e.layerType,
                layer = e.layer;

            drawnItems.addLayer(layer);
        });