Closed mirkomancin closed 6 years ago
I resolved with this:
Add @ViewChild(LeafletDirective) leafletDirective;
inside component class
And, when initialize map:
let _self = this;
this.leafletDirective.getMap().on(L.Draw.Event.CREATED, function(event) {
console.log("CREATED");
console.log(event);
console.log(event.layer);
if (event.layer instanceof L.Polygon) {
console.log("POLYGON");
let p = L.polygon(event.layer);
event.layer.getLatLngs()[0].forEach(function(latLng){
console.log(latLng);
});
}
});
You have a few options here to get a reference to the map.
As you found, you can get a reference to the LeafletDirective
instance using @ViewChild
.
You can also use (leafletMapReady)
which is an event that is emitted right after the map is created (see README for details) to get direct access to the map instance.
You could also make a custom directive that you add to the same element, and then use constructor injection to inject the LeafletDirective
into the custom directive.
Once you have the reference to the map, you can register a callback on the draw events. If you run into issues getting change detection to occur for changes you make in the callbacks, you should check the part of the README that talks about Angular zones and change detection.
Closing as I believe the question has been addressed.
How to retrieve layer that drawing and how to register event Draw.Event.CREATED or Draw.Event.EDITED callback?
I found few example on the web but they all use L.map object. With yours module I can't retrieve this object. How to do that?
I want to retrieve area and check if, in the area drawn, there are some markers already fixed.
Thanks