|
7 | 7 | import json |
8 | 8 | import operator |
9 | 9 | 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 | + get_args, |
| 21 | +) |
11 | 22 |
|
12 | 23 | import numpy as np |
13 | 24 | import requests |
|
34 | 45 | TypeJsonValue, |
35 | 46 | TypeLine, |
36 | 47 | TypePathOptions, |
| 48 | + TypePosition, |
37 | 49 | _parse_size, |
38 | 50 | escape_backticks, |
39 | 51 | get_bounds, |
@@ -1831,7 +1843,7 @@ def __init__(self, popup: Union[IFrame, Html, str, None] = None): |
1831 | 1843 | if isinstance(popup, Element): |
1832 | 1844 | popup = popup.render() |
1833 | 1845 | if popup: |
1834 | | - self.popup = "`" + escape_backticks(popup) + "`" |
| 1846 | + self.popup = "`" + escape_backticks(popup) + "`" # type: ignore |
1835 | 1847 | else: |
1836 | 1848 | self.popup = '"Latitude: " + lat + "<br>Longitude: " + lng ' |
1837 | 1849 |
|
@@ -2009,3 +2021,61 @@ def __init__( |
2009 | 2021 | out.setdefault(cm(color), []).append([[lat1, lng1], [lat2, lng2]]) |
2010 | 2022 | for key, val in out.items(): |
2011 | 2023 | self.add_child(PolyLine(val, color=key, weight=weight, opacity=opacity)) |
| 2024 | + |
| 2025 | + |
| 2026 | +class Control(JSCSSMixin, MacroElement): |
| 2027 | + """ |
| 2028 | + Add a Leaflet Control object to the map |
| 2029 | +
|
| 2030 | + Parameters |
| 2031 | + ---------- |
| 2032 | + control: str |
| 2033 | + The javascript class name of the control to be rendered. |
| 2034 | + position: str |
| 2035 | + One of "bottomright", "bottomleft", "topright", "topleft" |
| 2036 | +
|
| 2037 | + Examples |
| 2038 | + -------- |
| 2039 | +
|
| 2040 | + >>> import folium |
| 2041 | + >>> from folium.features import Control, Marker |
| 2042 | + >>> from folium.plugins import Geocoder |
| 2043 | +
|
| 2044 | + >>> m = folium.Map( |
| 2045 | + ... location=[46.603354, 1.8883335], attr=None, zoom_control=False, zoom_start=5 |
| 2046 | + ... ) |
| 2047 | + >>> Control("Zoom", position="topleft").add_to(m) |
| 2048 | + """ |
| 2049 | + |
| 2050 | + _template = Template( |
| 2051 | + """ |
| 2052 | + {% macro script(this, kwargs) %} |
| 2053 | + var {{ this.get_name() }} = new L.Control.{{this._name}}( |
| 2054 | + {% for arg in this.args %} |
| 2055 | + {{ arg | tojavascript }}, |
| 2056 | + {% endfor %} |
| 2057 | + {{ this.options|tojavascript }} |
| 2058 | + ).addTo({{ this._parent.get_name() }}); |
| 2059 | + {% endmacro %} |
| 2060 | + """ |
| 2061 | + ) |
| 2062 | + |
| 2063 | + def __init__( |
| 2064 | + self, |
| 2065 | + control: Optional[str] = None, |
| 2066 | + *args, |
| 2067 | + position: Optional[TypePosition] = None, |
| 2068 | + **kwargs, |
| 2069 | + ): |
| 2070 | + super().__init__() |
| 2071 | + if control: |
| 2072 | + self._name = control |
| 2073 | + |
| 2074 | + if position is not None: |
| 2075 | + position = position.lower() # type: ignore |
| 2076 | + if position not in (args := get_args(TypePosition)): |
| 2077 | + raise TypeError(f"position must be one of {args}") |
| 2078 | + kwargs["position"] = position |
| 2079 | + |
| 2080 | + self.args = args |
| 2081 | + self.options = remove_empty(**kwargs) |
0 commit comments