@@ -122,3 +122,69 @@ Plugins
122122 - Display gridded vector data (GeoJSON or TopoJSON sliced with geojson-vt, or protobuf vector tiles).
123123 * - :doc: `WMS Time Dimension <plugins/WmsTimeDimension >`
124124 - Create a time-aware WmsTileLayer.
125+
126+ .. _custom_controls :
127+
128+ Custom Controls via ``Control `` (add text/UI without a full plugin)
129+ ===================================================================
130+
131+ Folium’s ``Control `` (Leaflet’s ``L.Control ``) lets you add small custom UI elements—
132+ like always-visible text, badges, or buttons—**without ** writing a full Folium plugin.
133+ Below is a minimal example that injects a Leaflet control containing text.
134+
135+ .. code-block :: python
136+
137+ from folium import Map
138+ from branca.element import Element
139+
140+ # 1) Create a map
141+ m = Map(location = [0 , 0 ], zoom_start = 2 )
142+
143+ # 2) Get the JS map variable name Folium uses (needed to attach the control)
144+ map_var = m.get_name()
145+
146+ # 3) Inject a small Leaflet control that renders HTML text
147+ control_js = f """
148+ <script>
149+ // Define a small custom control
150+ var CustomText = L.Control.extend( {{
151+ onAdd: function(map) {{
152+ var div = L.DomUtil.create('div');
153+ div.innerHTML = 'My Label';
154+ div.style.background = 'white';
155+ div.style.padding = '4px 8px';
156+ div.style.border = '1px solid #ccc';
157+ div.style.borderRadius = '4px';
158+ div.style.font = '14px/1.2 sans-serif';
159+ return div;
160+ }} ,
161+ onRemove: function(map) {{}}
162+ }} );
163+
164+ // Factory and add to the map in the top-right corner
165+ L.control.customText = function(opts) {{
166+ return new CustomText(opts);
167+ }} ;
168+ L.control.customText( {{ position: 'topright' }} ).addTo( { map_var} );
169+ </script>
170+ """
171+
172+ m.get_root().html.add_child(Element(control_js))
173+ m.save(" map_with_custom_text_control.html" )
174+
175+ **Notes & tips **
176+
177+ - This approach is best for **small UI ** (labels, badges, short instructions).
178+ - For **text tied to a geographic point **, see :py:class: `folium.features.DivIcon `.
179+ - If your control overlaps with other UI, change the Leaflet position
180+ (``'topleft' ``, ``'topright' ``, ``'bottomleft' ``, ``'bottomright' ``) or use custom CSS.
181+ - For larger features, consider wrapping JS in a proper Folium element (e.g. a
182+ :class: `branca.element.MacroElement `) and reusing it across maps.
183+
184+ .. seealso ::
185+
186+ Leaflet Controls: https://leafletjs.com/reference.html#control
187+ • API: :py:class: `folium.features.Control `
188+
189+
190+
0 commit comments