javascript - Leaflet.draw编辑激活使用方法

标签 javascript leaflet

我不确定我需要做什么才能让 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/

相关文章:

javascript - dijit/MenuItem 已禁用但不会自动变灰

javascript - 元素不可见错误

javascript - 如何将 src 的值放入输入然后将其显示在另一页上?

r - Leaflet Map - 第二个多边形使第一层不可点击

jquery - Leaflet不同的悬停和点击事件

ionic-framework - Leaflet JS和Ionic禁用 map 水印

javascript - 字段变为空白

javascript - 从 json 填充可观察数组

javascript - React Leaflet : Is there a way to make an onClick method that adds a marker and also updates the state with that markers' location?(React 初学者)

R 传单突出显示选项