Can't get _leaflet_id of a drawn object with leaflet draw

2,355 views
Skip to first unread message

Khalil SLAMA

unread,
Mar 11, 2015, 11:39:46 AM3/11/15
to leafl...@googlegroups.com
Hi !

I need to get the "_leaflet_id" of each drawed feature in the map, but the problem is that I get "undefined" each time.

Please find below my code:
var featureGroup = L.featureGroup().addTo(map);
        var drawControl = new L.Control.Draw({
                position : 'topleft',
                draw: {
                        polygon : false
                },
                edit: {
                        featureGroup : featureGroup
                }
        });
        map.on('draw:created', function (e) {
                var type = e.layerType,
                        layer = e.layer;
                if (type === 'circle')
                {
                        console.log(layer._leaflet_id);
                        console.log(layer);

                }
                featureGroup.addLayer(e.layer);
        });
        map.on('draw:deleted', function (e) {
                var type = e.layerType,
                        layer = e.layer;
                if (type === 'circle')
                {
                        //circles.remove(layer._leaflet_id);
                }
                featureGroup.removeLayer(e.layer);
        });


When I do layer._leaflet_id I get : undefined
But, whith console.log(layer), I can see the _leaflet_id of the drawn object (_leaflet_id : 42) :
o.Class.extend.e {options: Object, _latlng: o.LatLng, _mRadius: 1369.5112680602524, _initHooksCalled: true, editing: o.Class.extend.e…}
_container: g
_initHooksCalled: true
_latlng: o.LatLng
_leaflet_events: Object
_leaflet_id: 42
_mRadius: 1369.5112680602524
_map: o.Class.extend.e
_path: path
_point: o.Point
_radius: 54
editing: o.Class.extend.e
options: Object
__proto__: o.Class.extend.i




Why I can't get it ? Is there any other solution to get this id please ?

Many thanks,

Pat Keller

unread,
Mar 12, 2015, 10:04:32 AM3/12/15
to leafl...@googlegroups.com
I don't see anything wrong with your code. I just tried it and its working for me. What happens if you set a breakpoint in the draw:created event and check the value in the console? What do you get if you try to do console.log(e.layer._leaflet_id)?

Paul Verhoeven

unread,
Mar 13, 2015, 12:34:32 AM3/13/15
to leafl...@googlegroups.com
If you comment out 

featureGroup.addLayer(e.layer);

You'll see that e.layer never gets a _leaflet_id which gives you your answer. It gets assigned by adding it to the featureGroup

Khalil SLAMA

unread,
Mar 13, 2015, 5:45:02 AM3/13/15
to leafl...@googlegroups.com
Yes, I saw this detail after doing some tests with featureGroup. Now I can get the _leaflet_id using the featureGroup.

Thank you Paul and Pat for your responses.

revathi kanakapathy

unread,
Feb 22, 2017, 2:34:13 AM2/22/17
to Leaflet
am too having the same problem. Getting _leaflet_id as undefined .
map.on('draw:deleted', function (e) {

       
var layers = e.layers;
       console
.log(layers._layers._leaflet_id);
});


Pat Keller

unread,
Feb 22, 2017, 9:00:46 AM2/22/17
to Leaflet
Did you read the above posts? Are you adding your layer to a featureGroup?
Reply all
Reply to author
Forward
0 new messages