|
| 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 | +```python |
| 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 | + "MyPlugin", |
| 40 | + position="topright", |
| 41 | + # Any plugin options become JS options. |
| 42 | + foo="bar", |
| 43 | +) |
| 44 | + |
| 45 | +# Add the plugin's JS/CSS assets. |
| 46 | +control.add_js_link("my-plugin", "https://example.com/leaflet.my-plugin.min.js") |
| 47 | +control.add_css_link("my-plugin", "https://example.com/leaflet.my-plugin.css") |
| 48 | + |
| 49 | +control.add_to(m) |
| 50 | +m |
| 51 | +``` |
| 52 | + |
| 53 | +For reusable plugins, consider creating a dedicated `folium.plugins` class. For |
| 54 | +one-off integrations, `Control` keeps the wiring minimal. |
0 commit comments