Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding properties to a leaflet layer that will become geojson options

Tags:

leaflet

mapbox

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?

like image 688
Justin Dearing Avatar asked Apr 19 '15 21:04

Justin Dearing


People also ask

How do I add a geoJSON to a Leaflet?

To create it and add it to a map, we can use the following code: L. geoJSON(geojsonFeature). addTo(map);

What is Tilelayer in Leaflet?

A LeafletJS plugin to appy WebGL shaders to your tiles. With this plugin, you can apply colour transforms to your tiles, merge two or more tiles with a custom function, perform on-the-fly hillshading, or create synthetic tile layers based only on the map coordinates.


1 Answers

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

like image 116
ghybs Avatar answered Jan 02 '23 23:01

ghybs