5

I always add markers to a Leaflet layer group like this:

someMarker.addTo(someLayerGroup);

But looking at the docs I just realized that you can also do:

someLayerGroup.addLayer(someMarker);

Are there benefits of using one way over the other?

1 Answer 1

2

When I learned Leaflet I was told that although both ways can work, it generally makes more sense to set it up the first way you described so that everything that is inherited from Layer can be brought in smoothly. This also works best when there are several markers, layers, and or layer groups in the same map.

However, this really comes into play when using Control Layer Groups. If you aren't familiar with Control Layers I recommend playing around with them and reading up on the documentation so you can see how the ordering works. Good luck.

https://leafletjs.com/reference-1.3.2.html#control

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.