将属性添加到将成为 geojson 选项的传单层

Adding properties to a leaflet layer that will become geojson options

假设我在 mapbox 地图上绘制了一个形状,并在 draw:crated 事件中执行此操作:

 e.layer.properties = {};
 e.layer.properties.myId = 'This is myId';

如果我执行 featureGroup.toGeoJSON() geojson 功能有一个空属性对象。有什么方法可以配置传单层,以便在将其转换为 geoJson 时设置某些属性?

您可以修改传单源或编写自己的函数来处理图层并设置您要查找的属性。

实际上,技巧就是定义层 feature 及其 type(必须是 "Feature")和 properties(使用后者记录任何内容您需要的信息)。

map.on('draw:created', function (event) {
    var layer = event.layer,
        feature = layer.feature = layer.feature || {}; // Initialize feature

    feature.type = feature.type || "Feature"; // Initialize feature.type
    var props = feature.properties = feature.properties || {}; // Initialize feature.properties
    props.myId = 'This is myId';
    drawnItems.addLayer(layer); // whatever you want to do with the created layer
});

另见 and update properties of geojson to use it with leaflet