Skip to content

Commit 6000781

Browse files
committed
Add GlobalSwitches element to head of map
Gives ability to set Leaflet's global switches: - prefer_canvas: Forces Leaflet to use the Canvas back-end (if available) for vector layers instead of SVG - no_touch: Forces Leaflet to not use touch events even if it detects them - disable_3d: 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
1 parent 19916a0 commit 6000781

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

folium/map.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ def __init__(self, location=None, width='100%', height='100%',
142142
zoom_start=10, continuous_world=False, world_copy_jump=False,
143143
no_wrap=False, attr=None, min_lat=-90, max_lat=90,
144144
min_lon=-180, max_lon=180, max_bounds=True,
145-
detect_retina=False, crs='EPSG3857', control_scale=False):
145+
detect_retina=False, crs='EPSG3857', control_scale=False,
146+
prefer_canvas=False, no_touch=False, disable_3d=False):
146147
super(LegacyMap, self).__init__()
147148
self._name = 'Map'
148149
self._env = ENV
@@ -176,6 +177,8 @@ def __init__(self, location=None, width='100%', height='100%',
176177
self.crs = crs
177178
self.control_scale = control_scale
178179

180+
self.global_switches = GlobalSwitches(prefer_canvas, no_touch, disable_3d)
181+
179182
if tiles:
180183
self.add_tile_layer(
181184
tiles=tiles, min_zoom=min_zoom, max_zoom=max_zoom,
@@ -254,6 +257,9 @@ def render(self, **kwargs):
254257
assert isinstance(figure, Figure), ("You cannot render this Element "
255258
"if it's not in a Figure.")
256259

260+
# Set global switches
261+
figure.header.add_child(self.global_switches, name='global_switches')
262+
257263
# Import Javascripts
258264
for name, url in _default_js:
259265
figure.header.add_child(JavascriptLink(url), name=name)
@@ -284,6 +290,24 @@ def render(self, **kwargs):
284290
super(LegacyMap, self).render(**kwargs)
285291

286292

293+
class GlobalSwitches(Element):
294+
def __init__(self, prefer_canvas=False, no_touch=False, disable_3d=False):
295+
super(GlobalSwitches, self).__init__()
296+
self._name = 'GlobalSwitches'
297+
298+
self.prefer_canvas = prefer_canvas
299+
self.no_touch = no_touch
300+
self.disable_3d = disable_3d
301+
302+
self._template = Template(
303+
'<script>'
304+
'L_PREFER_CANVAS = {% if this.prefer_canvas %}true{% else %}false{% endif %}; '
305+
'L_NO_TOUCH = {% if this.no_touch %}true{% else %}false{% endif %}; '
306+
'L_DISABLE_3D = {% if this.disable_3d %}true{% else %}false{% endif %};'
307+
'</script>'
308+
)
309+
310+
287311
class Layer(MacroElement):
288312
"""An abstract class for everything that is a Layer on the map.
289313
It will be used to define whether an object will be included in

0 commit comments

Comments
 (0)