0

I have the following code which is used the create a Leaflet layer control and keep it open by default:-

const layerControl = L.control.layers(baseLayers, null, {collapsed:false, position: 'bottomright'}).addTo(map);.

What is the function of the null value added to the code. If I remove the null I get an error :-

Uncaught TypeError: Cannot use 'in' operator to search for '_leaflet_id' in false. This error is visible from the browser console.

If I add an overlay layer to the code, I have to remove the null in order for the layer control widget to be added to the map with default setting:-

const layerControl = L.control.layers(baseLayers, overlays, {collapsed:false, position: 'bottomright'}).addTo(map);

I have tried to search the documentation and other online resources for the explanation but could not get any.

2

1 Answer 1

1

I am posting an explanation based on what I understood from the documentation link in the comment of @IvanSanchez. In case it might the useful for someone else.

I guess the null value acts as a placeholder for the omitted object when adding layer control options in the line of code

const layerControl = L.control.layers(baseLayers, null,  {collapsed:false, position: 'bottomright'}).addTo(map);

Below is the part of the documentation that expalins it properly

The first argument passed when creating the layers control is the base layers object. The second argument is the overlays object. Both arguments are optional: you can pass just a base layers object by omitting the second argument, or just an overlays objects by passing null as the first argument. In each case, the omitted layer type will not appear for the user to select.

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.