如何使用 Leaflet 一次只允许编辑一个特征/多边形? [英] How to allow only one feature/polygon to be edited at a time with Leaflet?
问题描述
这几天我一直在努力解决我的问题.
It's been days I'm trying to solve my problem.
我有一个来自 GeoJSON 的多边形层.我想用点击事件编辑我的多边形.当我点击一个多边形时,它变得可编辑,但我想要的是,当我点击另一个多边形时,第一个多边形不再处于可编辑模式.
I have a polygon layer from a GeoJSON. I want to edit my polygons with the click event. When I click on a polygon it becomes editable but what I want is that when I click on another polygon, the first polygon is no longer in editable mode.
OpenLayers 但自然没有 Leaflet.
OpenLayers but naturally does not Leaflet.
这是我的代码的摘录:
var editableLayers = new L.FeatureGroup().addTo(map);
var polygon_json;
$.ajax({
type: "GET",
dataType: "json",
url: "get_json.php",
success: function (response) {
meaux_json = L.geoJson(response, {
onEachFeature: onEachFeature
});
}
});
//edit the targeted polygon
function onEachFeature (feature, layer) {
editableLayers.addLayer(layer);
layer.on('click', function(e){
e.target.editing.enable();
});
}
一个人能够做到,但我很难理解如何:https://github.com/dwilhelm89/Ethermap
One person was able to do it but I am having difficulty understanding how : https://github.com/dwilhelm89/Ethermap
推荐答案
我觉得你很接近.在您的 onEachFeature 函数中,您应该存储被点击的特征,以便您可以在点击处理程序中启用/禁用编辑.
I think you are close. In your onEachFeature function you should store the feature that was clicked so you can enable/disable editing in the click handler.
var selectedFeature = null;
//edit the targeted polygon
function onEachFeature (feature, layer) {
editableLayers.addLayer(layer);
layer.on('click', function(e){
if(selectedFeature)
selectedFeature.editing.disable();
selectedFeature = e.target;
e.target.editing.enable();
});
}
这篇关于如何使用 Leaflet 一次只允许编辑一个特征/多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!