Skip to content

Draggable layer control #1760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 21, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions folium/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ class LayerControl(MacroElement):
autoZIndex : bool, default True
If true the control assigns zIndexes in increasing order to all of
its layers so that the order is preserved when switching them on/off.
draggable: bool, default False
By default the layer control has a fixed position. Set this argument
to True to allow dragging the control around.
**kwargs
Additional (possibly inherited) options. See
https://leafletjs.com/reference.html#control-layers
Expand All @@ -153,7 +156,7 @@ class LayerControl(MacroElement):
_template = Template(
"""
{% macro script(this,kwargs) %}
var {{ this.get_name() }} = {
var {{ this.get_name() }}_layers = {
base_layers : {
{%- for key, val in this.base_layers.items() %}
{{ key|tojson }} : {{val}},
Expand All @@ -165,12 +168,16 @@ class LayerControl(MacroElement):
{%- endfor %}
},
};
L.control.layers(
{{ this.get_name() }}.base_layers,
{{ this.get_name() }}.overlays,
let {{ this.get_name() }} = L.control.layers(
{{ this.get_name() }}_layers.base_layers,
{{ this.get_name() }}_layers.overlays,
{{ this.options|tojson }}
).addTo({{this._parent.get_name()}});

{%- if this.draggable %}
new L.Draggable({{ this.get_name() }}.getContainer()).enable();
{%- endif %}

{% endmacro %}
"""
)
Expand All @@ -180,13 +187,15 @@ def __init__(
position: str = "topright",
collapsed: bool = True,
autoZIndex: bool = True,
draggable: bool = False,
**kwargs: TypeJsonValue,
):
super().__init__()
self._name = "LayerControl"
self.options = parse_options(
position=position, collapsed=collapsed, autoZIndex=autoZIndex, **kwargs
)
self.draggable = draggable
self.base_layers: OrderedDict[str, str] = OrderedDict()
self.overlays: OrderedDict[str, str] = OrderedDict()

Expand Down