|
| 1 | +"""Add Locate control to folium Map. |
| 2 | +
|
| 3 | +Based on leaflet plugin: https://github.com/domoritz/leaflet-locatecontrol |
| 4 | +""" |
| 5 | + |
| 6 | +from branca.element import CssLink, Figure, JavascriptLink, MacroElement |
| 7 | + |
| 8 | +from jinja2 import Template |
| 9 | + |
| 10 | + |
| 11 | +class LocateControl(MacroElement): |
| 12 | + """Control plugin to geolocate the user. |
| 13 | +
|
| 14 | + Parameters |
| 15 | + ---------- |
| 16 | + options: dict, optional |
| 17 | + For possible options, see https://github.com/domoritz/leaflet-locatecontrol |
| 18 | +
|
| 19 | + Examples |
| 20 | + -------- |
| 21 | + >>> import folium |
| 22 | + >>> from folium.plugins import LocateControl |
| 23 | + >>> map = folium.Map() |
| 24 | + # With default settings |
| 25 | + >>> LocateControl().add_to(map) |
| 26 | +
|
| 27 | + # With custom options |
| 28 | + >>> options = {"position": "topright", |
| 29 | + ... "strings":{ |
| 30 | + ... "title":"Show my current location"}} |
| 31 | + >>> LocateControl(options=options).add_to(map) |
| 32 | +
|
| 33 | + For more info check: |
| 34 | + https://github.com/domoritz/leaflet-locatecontrol |
| 35 | +
|
| 36 | + """ |
| 37 | + |
| 38 | + _template = Template(u""" |
| 39 | + {% macro script(this, kwargs) %} |
| 40 | + var {{this.get_name()}} = L.control.locate( |
| 41 | + {{this.options|tojson}}).addTo({{this._parent.get_name()}}); |
| 42 | + {% endmacro %} |
| 43 | + """) |
| 44 | + |
| 45 | + def __init__(self, options=None): |
| 46 | + """Initialization.""" |
| 47 | + super(LocateControl, self).__init__() |
| 48 | + self._name = 'LocateControl' |
| 49 | + self.options = options or {} |
| 50 | + |
| 51 | + def render(self, **kwargs): |
| 52 | + super(LocateControl, self).render(**kwargs) |
| 53 | + figure = self.get_root() |
| 54 | + assert isinstance(figure, Figure), ('You cannot render this Element ' |
| 55 | + 'if it is not in a Figure.') |
| 56 | + |
| 57 | + figure.header.add_child( |
| 58 | + CssLink( |
| 59 | + "https://cdnjs.cloudflare.com/ajax/libs/leaflet-locatecontrol/0.66.2/L.Control.Locate.min.css")) # noqa |
| 60 | + figure.header.add_child(JavascriptLink( |
| 61 | + "https://cdnjs.cloudflare.com/ajax/libs/leaflet-locatecontrol/0.66.2/L.Control.Locate.min.js")) # noqa |
0 commit comments