Hi,
when rendering GeoJSON on a Leaflet.js map, I want to fire a custom event on marker drag that is propagated to an event listener on another GeoJSON feature. What I have now in GeoJSON options along these lines:
var opts = {
pointToLayer: function (feature, latlng) {
var marker = L.marker(latlng);
marker.on("drag", function (event) {
event.target._map.fire("customEvent");
});
}
return marker;
},
onEachFeature: function (feature, layer) {
layer.on("customEvent", function (event) {
console.log("Event received!");
});
}
}
};
There are at least 2 problems with this.
First, I haven't found a way how to fire an event on marker drag other than abusing the pointer to map: event.target._map.fire("customEvent"). Is there any better way?
Second, if the event listener is attached in onEachFeature, no event is received. However, if I attach the event listener directly to the map object, only then I can see the custom event is fired. How should I properly attach the event listener in onEachFeature?
Finally, is this a wrong approach to communication between GeoJSON features? I base my understanding on Leaflet.js reference, which explicitly mentions that onEachFeature is "Useful for attaching events and popups to features." I've tried to find examples of using events for communication between features, but I was unsuccessful. I'd appreciate any links to posts explaining how to go about this.
Best,
Jindřich
--
Jindřich Mynarz