12
votes

Lets say I draw a shape on a mapbox map, and do this on the draw:crated event:

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

If I do a featureGroup.toGeoJSON() the geojson features has an empty properties object. Is there any way I configure a leaflet layer so that when it is transformed to geoJson, it will have certain properties set?

2

2 Answers

16
votes

Actually, the trick is just to define the layer feature with its type (must be a "Feature") and properties (use the latter to record whatever information you need).

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

See also Leaflet Draw not taking properties when converting FeatureGroup to GeoJson and update properties of geojson to use it with leaflet

0
votes

You can either modify the leaflet source or write your own function to process the layers and set the properties that you are looking for.