How to allow editing only one object / polygon at a time using the "Flyer"?

These were the days when I try to solve my problem.

I have a polygon layer from GeoJSON. I want to edit my polygons using the click event. When I click on a polygon, it becomes editable, but I want the first polygon to no longer be edited when I click on another polygon.

OpenLayers, but naturally not Leaflet.

Here is an excerpt from my code:

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(); }); } 

One person was able to do this, but it's hard for me to understand how: https://github.com/dwilhelm89/Ethermap

+5
source share
2 answers

I think you are close. In your onEachFeature function, you must save the function that was pressed so that 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(); }); } 
+5
source
 layer.on('click', function(event){ editableLayers.eachLayer(function(layer) { layer.editing.disable() }) event.target.editing.enable(); }); 
0
source

Source: https://habr.com/ru/post/1206875/


All Articles