且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Mapbox和Leaflet Draw仅可编辑1组

更新时间:2023-01-01 23:05:16

首先创建两个要素组和一个图层控件,以便您可以在它们之间切换:

First create two featuregroups and a layercontrol so you can toggle between them:

var layerControl = new L.Control.Layers({
    'FeatureGroup 1': new L.FeatureGroup(),
    'FeatureGroup 2': new L.FeatureGroup()
}).addTo(map);

现在,您在这些之间进行切换,地图将触发一个basemapchanged事件,该事件包含当前选定的要素组,并将其存储在变量中:

Now then you toggle between those, the map fires a basemapchanged event which contains the currently selected featuregroup, store that in a variable:

var currentFeatureGroup;

map.on('baselayerchange', function (e) {
    currentFeatureGroup = e.layer;
});

绘制某些内容时,地图会触发另一个事件,您可以在其中处理绘制的特征.创建一个函数,该函数返回当前选定的要素组并将其存储在其中:

When you draw something, the map fires another event where you can process the drawn feature. Create a function that returns the currently selected featuregroup and store it into that:

function getCurrentFeatureGroup () {
    return currentFeatureGroup;
}

map.on('draw:created', function (e) {
    var featureGroup = getCurrentFeatureGroup();
    if (featureGroup instanceof L.FeatureGroup) {
        featureGroup.addLayer(e.layer);
    } else {
        alert('Please select a featuregroup');
    }
});

以下是有关Plunker的概念的示例: http://plnkr.co/edit/9ZEjuP ?p =预览

Here's an example of the concept on Plunker: http://plnkr.co/edit/9ZEjuP?p=preview