我不确定我需要做些什么来使leaflet.draw的编辑功能起作用。我创建了一个形状,但之后编辑按钮仍然显示为灰色。据我所知,我的代码与我读的相同。
// Initialise the FeatureGroup to store editable layers
var drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
// Initialise the draw control and pass it the FeatureGroup of editable layers
var drawControl = new L.Control.Draw({
edit: {
featureGroup: drawnItems
},
draw:{
rectangle: false,
circle: false
}
});
map.addControl(drawControl);
// On Shape/Line/Marker completion
map.on('draw:created', function (e) {
var type = e.layerType,
layer = e.layer;
if (type === 'marker') {
// Do marker specific actions
}
// Do whatever else you need to. (save to db, add to map etc)
map.addLayer(layer);
});
// On Editing Completion
map.on('draw:edited', function (e) {
var layers = e.layers;
layers.eachLayer(function (layer) {
//do whatever you want, most likely save back to db
});
});
我确保将功能组设置为自述文件非常清楚,这是必需的,但我无法找到任何其他内容。
答案 0 :(得分:2)
我最终解决了这个问题。您需要更改提供的代码中的代码,以便将形状添加到drawnItems
图层而不是map
。
// On Shape/Line/Marker completion
map.on('draw:created', function (e) {
var type = e.layerType,
layer = e.layer;
if (type === 'marker') {
// Do marker specific actions
}
// Do whatever else you need to. (save to db, add to map etc)
drawnItems.addLayer(layer) // previously map.addLayer(layer);
});