Leaflet / Leaflet.Editable

Make geometries editable in Leaflet.
http://leaflet.github.io/Leaflet.Editable/doc/api.html
559 stars 197 forks source link

Invalid geometries are not removed with stopDrawing() #51

Open hyperknot opened 8 years ago

hyperknot commented 8 years ago

At the moment, if the user starts to draw a geometry, and stops while it's not complete, by triggering stopDrawing(), the incomplete feature is saved to the feature layer, no matter if it is valid or invalid. (By invalid I mean having MIN_VERTEX vertices.)

I believe endDrawing() should be removing it, but somehow with stopDrawing(), endDrawing() isn't called.

hyperknot commented 8 years ago

The following events are triggered when I call editTools.stopDrawing() with a 2 vertex polygon:

yohanboniface commented 8 years ago

endDrawing only deletes the current drawn shape if invalid, but does not take care of removing the feature from the map. Maybe this can be added in case the feature is without any shape after that, but I'm not even sure it's to be added as default behaviour. We may think at some use cases where someone would want to do something else in such case. I need to think a bit more on that. One way to solve the issue for you is to listen for editable:drawing:cancel event, and control here the state of the feature.

hyperknot commented 8 years ago

So far the best solution I found is to check for e.layer._defaultShape() in editable:drawing:end, as by this time this will be an empty array if the shape was invalid.

m.Lmap.editTools.on('editable:drawing:end', function(e) {
    if (!e.layer._defaultShape().length) {
        console.log('removing layer');
        featuregroup.removeLayer(e.layer);
        return;
    }

    // do rest of processing
});
hyperknot commented 8 years ago

I just found that Measurable has pretty much the exact same solution implemented: https://github.com/umap-project/Leaflet.Measurable/blob/52d56e758e34e2761be4c8c878abe042eed37c62/Leaflet.Measurable.js#L116