如何使用Leaflet一次仅编辑一个要素/多边形? [英] How to allow only one feature/polygon to be edited at a time with Leaflet?

查看:423
本文介绍了如何使用Leaflet一次仅编辑一个要素/多边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好几天以来,我一直在努力解决自己的问题.

It's been days I'm trying to solve my problem.

我有一个来自GeoJSON的多边形图层.我想用click事件编辑多边形.当我单击一个多边形时,它变为可编辑状态,但是我想要的是,当我单击另一个多边形时,第一个多边形不再处于可编辑模式.

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,但自然不会传单.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆