Marcus Junius Brutus
Marcus Junius Brutus

Reputation: 27276

obtain the layers control from the map object

Say I add a Control.Layers object on a map like this:

L.control.layers(baseLayers, overlays).addTo(someMap)

When I am later given the someMap object what is the proper way to obtain the layer control from it? E.g. I was hoping something like:

let controls = someMap.getControls();

... where I would then presumably be able to iterate and use the typeof operator to find the control I am interested in.

The closest I have found in answering the question is this SO answer which suggests extending the L.Control class and overriding onAdd in order to store a custom property on the map object. I find it hard to be believe it needs to be so convoluted as that. Plus, even if that approach was taken, how I am supposed to know that my overridden onAdd method does everything that the implementation in the original Layers Control.Layers does?

Upvotes: 0

Views: 776

Answers (1)

YaFred
YaFred

Reputation: 10008

The way it is implemented, adding a control to a map just leaves the control object with reference to the map object.

This means that the map object is unaware of the controls it has created (EDIT I should say: controls that were added to the map)

I guess there was no need for that. If you have a use case that shows this is a problem, you should open an issue.

So, I'm afraid the answer you already have is the right one.

Upvotes: 2

Related Questions