Skip to content

Commit b119c13

Browse files
berrfredFrederic BerrouetConengmo
authored
Update Map zoom_control variable to also allow string to set position (#1884)
* Updated zoom_control variable to handle either True/False or position * Updated description and added check on valid position values --------- Co-authored-by: Frederic Berrouet <frederic.berrouet@telecomitalia.it> Co-authored-by: Frank Anema <33519926+Conengmo@users.noreply.github.com>
1 parent a00c3c3 commit b119c13

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

folium/folium.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ class Map(JSCSSMixin, MacroElement):
151151
Forces Leaflet to not use hardware-accelerated CSS 3D
152152
transforms for positioning (which may cause glitches in some
153153
rare environments) even if they're supported.
154-
zoom_control : bool, default True
155-
Display zoom controls on the map.
154+
zoom_control : bool or position string, default True
155+
Display zoom controls on the map. The default `True` places it in the top left corner.
156+
Other options are 'topleft', 'topright', 'bottomleft' or 'bottomright'.
156157
font_size : int or float or string (default: '1rem')
157158
The font size to use for Leaflet, can either be a number or a
158159
string ending in 'rem', 'em', or 'px'.
@@ -214,6 +215,10 @@ class Map(JSCSSMixin, MacroElement):
214215
L.control.scale().addTo({{ this.get_name() }});
215216
{%- endif %}
216217
218+
{%- if this.zoom_control_position %}
219+
L.control.zoom( { position: {{ this.zoom_control|tojson }} } ).addTo({{ this.get_name() }});
220+
{%- endif %}
221+
217222
{% if this.objects_to_stay_in_front %}
218223
function objects_in_front() {
219224
{%- for obj in this.objects_to_stay_in_front %}
@@ -256,7 +261,7 @@ def __init__(
256261
no_touch: bool = False,
257262
disable_3d: bool = False,
258263
png_enabled: bool = False,
259-
zoom_control: bool = True,
264+
zoom_control: Union[bool, str] = True,
260265
font_size: str = "1rem",
261266
**kwargs: TypeJsonValue,
262267
):
@@ -290,10 +295,21 @@ def __init__(
290295
self.crs = crs
291296
self.control_scale = control_scale
292297

298+
# Zoom control position specified ?
299+
if isinstance(zoom_control, str):
300+
self.zoom_control_position = True
301+
if zoom_control not in {"topleft", "topright", "bottomleft", "bottomright"}:
302+
raise ValueError(
303+
"Incorrect value for `zoom_control`, choose from 'topleft', 'topright', 'bottomleft' or 'bottomright'."
304+
)
305+
self.zoom_control = zoom_control
306+
else:
307+
self.zoom_control_position = False
308+
293309
self.options = parse_options(
294310
max_bounds=max_bounds_array,
295311
zoom=zoom_start,
296-
zoom_control=zoom_control,
312+
zoom_control=False if self.zoom_control_position else zoom_control,
297313
prefer_canvas=prefer_canvas,
298314
**kwargs,
299315
)

0 commit comments

Comments
 (0)