|
| 1 | +```{code-cell} ipython3 |
| 2 | +--- |
| 3 | +nbsphinx: hidden |
| 4 | +--- |
| 5 | +import folium |
| 6 | +from folium.features import Control |
| 7 | +``` |
| 8 | + |
| 9 | +# Controls |
| 10 | + |
| 11 | +Leaflet controls are UI elements anchored to the corners of a map (zoom, scale, |
| 12 | +custom buttons, etc.). Folium exposes a generic `Control` class that can render |
| 13 | +any `L.Control.<Name>` class. This is useful when you want to wire up a Leaflet |
| 14 | +plugin directly in user code without creating a new `folium.plugins` class. |
| 15 | + |
| 16 | +## Built-in controls |
| 17 | + |
| 18 | +```{code-cell} ipython3 |
| 19 | +m = folium.Map(location=[45, 0], zoom_start=4, zoom_control=False) |
| 20 | +
|
| 21 | +Control("Zoom", position="topleft").add_to(m) |
| 22 | +Control("Scale", position="bottomleft").add_to(m) |
| 23 | +
|
| 24 | +m |
| 25 | +``` |
| 26 | + |
| 27 | +## Leaflet plugin controls |
| 28 | + |
| 29 | +If a Leaflet plugin exposes a `L.Control.<Name>` class, you can wire it up |
| 30 | +directly and attach its JS/CSS assets to the map. |
| 31 | + |
| 32 | +```{code-cell} ipython3 |
| 33 | +import folium |
| 34 | +from folium.features import Control |
| 35 | +
|
| 36 | +m = folium.Map(location=[45, 0], zoom_start=4) |
| 37 | +
|
| 38 | +control = Control( |
| 39 | + "fullscreen", |
| 40 | + position="topright", |
| 41 | + # Any plugin options become JS options. |
| 42 | + title="View Fullscreen", |
| 43 | + titleCancel="Exit Fullscreen", |
| 44 | +) |
| 45 | +
|
| 46 | +# Add the plugin's JS/CSS assets. |
| 47 | +control.add_js_link( |
| 48 | + "leaflet-fullscreen", |
| 49 | + "https://unpkg.com/leaflet-fullscreen@1.6.0/dist/leaflet.fullscreen.umd.js", |
| 50 | +) |
| 51 | +control.add_css_link( |
| 52 | + "leaflet-fullscreen", |
| 53 | + "https://unpkg.com/leaflet.fullscreen@1.6.0/Control.FullScreen.css", |
| 54 | +) |
| 55 | +
|
| 56 | +control.add_to(m) |
| 57 | +m |
| 58 | +``` |
| 59 | + |
| 60 | +For reusable plugins, consider creating a dedicated `folium.plugins` class. For |
| 61 | +one-off integrations, `Control` keeps the wiring minimal. |
0 commit comments