Skip to content
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

Fit enabled overlays #1693

Merged
merged 5 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
use new class FitSelected
  • Loading branch information
Conengmo committed Jan 7, 2023
commit af7ac5e4c1134513fcc7d2507b6c5d7b551dbb28
53 changes: 53 additions & 0 deletions folium/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -1929,3 +1929,56 @@ def __init__(
out.setdefault(cm(color), []).append([[lat1, lng1], [lat2, lng2]])
for key, val in out.items():
self.add_child(PolyLine(val, color=key, weight=weight, opacity=opacity))


class FitSelected(MacroElement):
"""Fit the bounds of the maps to the enabled overlays.

Parameters
----------
padding: int, default 25
Sets the amount of padding in the top left and bottom right corners of
a map container that shouldn't be accounted for when setting the view
to fit bounds.
max_zoom: int, optional
The maximum possible zoom to use when fitting to the bounds.
fly: bool, default False
Use a smoother, longer animation.
fit_on_map_load: bool, default True
Apply the fit when initially loading the map.
"""

_template = Template(
"""
{% macro script(this, kwargs) %}
function customFlyToBounds() {
let bounds = L.latLngBounds([]);
{{ this._parent.get_name() }}.eachLayer(function(layer) {
if (typeof layer.getBounds === 'function') {
bounds.extend(layer.getBounds());
}
});
if (bounds.isValid()) {
{{ this._parent.get_name() }}.{{ this.method }}(bounds, {{ this.options|tojson }});
}
}
{{ this._parent.get_name() }}.on('overlayadd', customFlyToBounds);
{%- if this.fit_on_map_load %}
customFlyToBounds();
{%- endif %}
{% endmacro %}
"""
)

def __init__(
self,
padding: int = 25,
max_zoom: Optional[int] = None,
fly: bool = False,
fit_on_map_load: bool = True,
):
super().__init__()
self._name = "FlyToSelected"
self.method = "flyToBounds" if fly else "fitBounds"
self.fit_on_map_load = fit_on_map_load
self.options = parse_options(padding=(padding, padding), max_zoom=max_zoom)
18 changes: 0 additions & 18 deletions folium/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,6 @@ class LayerControl(MacroElement):
{{ val }}.remove();
{%- endfor %}

{%- if this.fly_to_selected %}
function customFlyToBounds() {
let bounds = L.latLngBounds([]);
{{ this._parent.get_name() }}.eachLayer(function(layer) {
if (typeof layer.getBounds === 'function') {
bounds.extend(layer.getBounds());
}
});
if (bounds.isValid()) {
{{ this._parent.get_name() }}.flyToBounds(bounds, {padding: [25, 25]});
}
}
{{ this._parent.get_name() }}.on('overlayadd', customFlyToBounds);
customFlyToBounds();
{%- endif %}

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