Skip to content

Commit 40167e6

Browse files
committed
Add leaflet control
1 parent eda6118 commit 40167e6

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

folium/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
ClickForLatLng,
1818
ClickForMarker,
1919
ColorLine,
20+
Control,
2021
CustomIcon,
2122
DivIcon,
2223
GeoJson,
@@ -64,6 +65,7 @@
6465
"ClickForLatLng",
6566
"ColorLine",
6667
"ColorMap",
68+
"Control",
6769
"CssLink",
6870
"CustomIcon",
6971
"Div",

folium/features.py

+62-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@
77
import json
88
import operator
99
import warnings
10-
from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union
10+
from typing import (
11+
Any,
12+
Callable,
13+
Dict,
14+
Iterable,
15+
List,
16+
Optional,
17+
Sequence,
18+
Tuple,
19+
Union,
20+
)
1121

1222
import numpy as np
1323
import requests
@@ -1992,3 +2002,54 @@ def __init__(
19922002
out.setdefault(cm(color), []).append([[lat1, lng1], [lat2, lng2]])
19932003
for key, val in out.items():
19942004
self.add_child(PolyLine(val, color=key, weight=weight, opacity=opacity))
2005+
2006+
2007+
class Control(JSCSSMixin, MacroElement):
2008+
"""
2009+
Add a Leaflet Control object to the map
2010+
2011+
Parameters
2012+
----------
2013+
control: str
2014+
The javascript class name of the control to be rendered.
2015+
position: str
2016+
One of "bottomright", "bottomleft", "topright", "topleft"
2017+
2018+
Examples
2019+
--------
2020+
2021+
>>> import folium
2022+
>>> from folium.features import Control, Marker
2023+
>>> from folium.plugins import Geocoder
2024+
2025+
>>> m = folium.Map(
2026+
... location=[46.603354, 1.8883335], attr=None, zoom_control=False, zoom_start=5
2027+
... )
2028+
>>> Control("Zoom", position="topleft").add_to(m)
2029+
"""
2030+
2031+
_template = Template(
2032+
"""
2033+
{% macro script(this, kwargs) %}
2034+
var {{ this.get_name() }} = new L.Control.{{this._name}}(
2035+
{% for arg in this.args %}
2036+
{{ arg | tojavascript }},
2037+
{% endfor %}
2038+
{{ this.options|tojavascript }}
2039+
).addTo({{ this._parent.get_name() }});
2040+
{% endmacro %}
2041+
"""
2042+
)
2043+
2044+
def __init__(
2045+
self,
2046+
control: Optional[str] = None,
2047+
*args,
2048+
**kwargs,
2049+
):
2050+
super().__init__()
2051+
if control:
2052+
self._name = control
2053+
2054+
self.args = args
2055+
self.options = remove_empty(**kwargs)

0 commit comments

Comments
 (0)