我不确定我需要做什么才能让 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
});
});
我确定我已经设置了功能组,因为自述文件非常清楚地表明这是必需的,但我找不到关于它的任何其他内容。
最佳答案
我最终解决了这个问题。您需要更改提供的代码以将形状添加到 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);
});
关于javascript - Leaflet.draw编辑激活使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29593226/