Skip to content

Commit 283e747

Browse files
committed
Add options parameter to LocateControl plugin
1 parent bd4ac9f commit 283e747

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

folium/plugins/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from folium.plugins.fullscreen import Fullscreen
2020
from folium.plugins.heat_map import HeatMap
2121
from folium.plugins.heat_map_withtime import HeatMapWithTime
22+
from folium.plugins.locate_control import LocateControl
2223
from folium.plugins.marker_cluster import MarkerCluster
2324
from folium.plugins.measure_control import MeasureControl
2425
from folium.plugins.minimap import MiniMap
@@ -45,6 +46,7 @@
4546
'Fullscreen',
4647
'HeatMap',
4748
'HeatMapWithTime',
49+
'LocateControl',
4850
'MarkerCluster',
4951
'MeasureControl',
5052
'MiniMap',

folium/plugins/locate_control.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)