javascript - Leaflet Draw "Cannot read property ' enable' of undefined"向geoJSON层添加控件

标签 javascript leaflet leaflet.draw

我正在尝试对从数据库加载的多边形使用传单的编辑功能。当我点击传单的编辑按钮时,出现错误
无法读取未定义的属性“enable”

This thread描述了一个类似的问题,用户ddproxy说

"Since FeatureGroup extends LayerGroup You can walk through the layers presented and add them individually to the FeatureGroup used for Leaflet.draw"

我很困惑他所说的“遍历”是什么意思,我以为我在添加一个图层组,所以我不确定我会遍历什么。这是否与我将多边形添加为 geoJSON 对象有关?
将多边形添加到 map ,绑定(bind)它们的弹出窗口,并为它们分配自定义颜色,仅供引用。

相关代码如下:

<script>
window.addEventListener("load", function(event){
    //other stuff
    loadHazards(); 

});

//next 6 lines siply add map to page
var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
var osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors'
var osm = L.tileLayer(osmUrl, { maxZoom: 18, attribution: osmAttrib})
var map = new L.Map('map', { center: new L.LatLng(39.255467, -76.711964), zoom: 16 })

osm.addTo(map);

var drawnItems = L.featureGroup().addTo(map);
var Hazards = L.featureGroup().addTo(map);

L.control.layers({
        'osm': osm.addTo(map)
        },
        {
           'drawlayer': drawnItems,
           "Hazards" : Hazards,
           "Tickets": Tickets
         },

         {
           position: 'topleft', collapsed: false
         }
         ).addTo(map);

map.addControl(new L.Control.Draw({
    edit: {
        featureGroup: Hazards,
        poly: {
            allowIntersection: false
        }
    },
    draw: {
        polygon: {
            allowIntersection: false,
            showArea: true
        },
        rectangle:false,
        circle:false,
        circlemarker:false
    }
}));

map.on(L.Draw.Event.CREATED, function (event) {
    var layer = event.layer;
    drawnItems.addLayer(layer);
});

</script>

还有 loadHazards() 函数:

function loadHazards(){
$.ajax({
    type: 'GET',
    url:'/loadPolygonFromDatabase',
    success : function(polygons){           
        polygons = JSON.parse(polygons);

        var toAdd = [];
        for (i in polygons){

            var item = {
                    "type" : "Feature",
                    "properties":{
                        "category":"",
                        "description":"",
                        "ID":""
                     },
                     "geometry" : {
                        "type":"Polygon",
                        "coordinates":[],

                    }

            };

            item["geometry"]["coordinates"][0] = polygons[i]["coordinates"];
            item["properties"]["category"]     = polygons[i]["category"];
            item["properties"]["description"]  = polygons[i]["description"];
            item["properties"]["ID"]  = polygons[i]["ID"];
            toAdd.push(item);

        }

        //Add information to popup
        var layerGroup = L.geoJSON(toAdd, {
            onEachFeature: function (feature, layer) {
                layer.bindPopup(  '<h1>' + feature.properties.category + '</h1>'
                                + '<p>'  + feature.properties.description + '</p>');
                layer.id = feature.properties.ID;

          },
          style: function(feature){
             switch (feature.properties.category) {
                case 'Rabid_Beavers': return {color: "#663326"};
                case 'Fire':   return {color: "#ff0000"};
                case 'Flood':   return {color: "#0000ff"};
            }
          }
        }).addTo(Hazards);

    }
});
}

提前致谢!

最佳答案

如@ghybs 所述,Leaflet.Draw 不支持组或多边形。我需要相同的功能,所以几年前我创建了支持孔、MultiPolygons、GeoJSON 和 LayerGroups 的 Leaflet-Geoman(以前称为 leaflet.pm):

enter image description here

https://github.com/geoman-io/leaflet-geoman

希望对您有所帮助。

关于javascript - Leaflet Draw "Cannot read property ' enable' of undefined"向geoJSON层添加控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53463078/

相关文章:

javascript - 如何在 Extjs 4 中使用嵌套的 JSON 填充表单

javascript - 是否可以在 Leaflet.Draw 中设置大小和数量限制?

javascript - 在leaft.draw中,使用事件draw :deletestop,如何获取我想要删除的特定图层

javascript - Apollo 奇怪地改变了查询结果

javascript - 看起来很奇怪的 Javascript for 循环

node.js - Turf.js 合并寻找 GeoJSON 中的功能

javascript - 使用模块模式在传单 map 对象上运行方法

javascript - 如何使用不同的工具提示插件模仿 Leaflet.Draw 工具提示样式?

javascript - 无法从 VueJS 监听 Bootstrap 按钮组内的点击事件

传单 js : how to create tooltips for L. CircleMarker?