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

Update Map zoom_control variable to also allow string to set position #1884

Merged
Merged
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
19 changes: 15 additions & 4 deletions folium/folium.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ class Map(JSCSSMixin, MacroElement):
Forces Leaflet to not use hardware-accelerated CSS 3D
transforms for positioning (which may cause glitches in some
rare environments) even if they're supported.
zoom_control : bool, default True
Display zoom controls on the map.
zoom_control : bool or position string, default True
Display zoom controls on the map, eventually specifying its position.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Display zoom controls on the map, eventually specifying its position.
Display zoom controls on the map. The default `True` places it in the top left corner.
Other options are 'topleft', 'topright', 'bottomleft' or 'bottomright'.

**kwargs
Additional keyword arguments are passed to Leaflets Map class:
https://leafletjs.com/reference.html#map
Expand Down Expand Up @@ -210,6 +210,10 @@ class Map(JSCSSMixin, MacroElement):
L.control.scale().addTo({{ this.get_name() }});
{%- endif %}

{%- if this.zoom_control_position %}
L.control.zoom( { position: {{ this.zoom_control|tojson }} } ).addTo({{ this.get_name() }});
{%- endif %}

{% if this.objects_to_stay_in_front %}
function objects_in_front() {
{%- for obj in this.objects_to_stay_in_front %}
Expand Down Expand Up @@ -252,7 +256,7 @@ def __init__(
no_touch: bool = False,
disable_3d: bool = False,
png_enabled: bool = False,
zoom_control: bool = True,
zoom_control: Union[bool, str] = True,
**kwargs: TypeJsonValue,
):
super().__init__()
Expand Down Expand Up @@ -284,10 +288,17 @@ def __init__(
self.crs = crs
self.control_scale = control_scale

# Zoom control position specified ?
if isinstance(zoom_control, str):
self.zoom_control_position = True
self.zoom_control = zoom_control
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe useful to add a check here for valid values? Now if a users makes an error, they just get a grey screen. For example something like:

Suggested change
self.zoom_control = zoom_control
self.zoom_control = zoom_control
if zoom_control not in {'topleft', 'topright', 'bottomleft', 'bottomright'}:
raise ValueError("Incorrect value for `zoom_control`, choose from 'topleft', 'topright', 'bottomleft' or 'bottomright'.")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had considered introducing a check on valid position values but looking at various plugins I could not find such a check so I thought it was considered as unnecessary ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, this code base grew quite organically over time, so it's not always consistent. Thanks for putting it in!

else:
self.zoom_control_position = False

self.options = parse_options(
max_bounds=max_bounds_array,
zoom=zoom_start,
zoom_control=zoom_control,
zoom_control=False if self.zoom_control_position else zoom_control,
prefer_canvas=prefer_canvas,
**kwargs,
)
Expand Down