Skip to content

Commit 61daf0c

Browse files
fullonicConengmo
authored andcommitted
Add auto_start parameter to locate control plugin (#1220)
The locate control plugin doesn't provide a way of auto start, being necessary the user click on plugin button to get they position showed on map. However the Leaflet plugin gives the possibility of having a auto start simply by doing lc.start(); after plugin is initiation.
1 parent f000f2f commit 61daf0c

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

examples/Plugins.ipynb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,10 @@
764764
"## Locate control\n",
765765
"\n",
766766
"Adds a control button that when clicked, the user device geolocation is displayed.\n",
767+
"For list of all possible keyword options see:\n",
768+
"https://github.com/domoritz/leaflet-locatecontrol#possible-options\n",
767769
"\n",
768-
"To work properly in production, the connection needs to be encrypted, otherwise browser will not\n",
770+
"To work properly in production, the connection needs to be encrypted (HTTPS), otherwise browser will not\n",
769771
"allow users to share their location."
770772
]
771773
},
@@ -793,6 +795,9 @@
793795
"\n",
794796
"plugins.LocateControl().add_to(m)\n",
795797
"\n",
798+
"# If you want get the user device positon after load the map, set auto_start=True\n",
799+
"plugins.LocateControl(auto_start=True).add_to(m)\n",
800+
"\n",
796801
"m"
797802
]
798803
}

folium/plugins/locate_control.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class LocateControl(MacroElement):
2020
2121
Parameters
2222
----------
23+
auto-start : bool, default False
24+
When set to True, plugin will be activated on map loading and search for user position.
25+
Once user location is founded, the map will automatically centered in using user coordinates.
2326
**kwargs
2427
For possible options, see https://github.com/domoritz/leaflet-locatecontrol
2528
@@ -45,12 +48,16 @@ class LocateControl(MacroElement):
4548
var {{this.get_name()}} = L.control.locate(
4649
{{this.options | tojson}}
4750
).addTo({{this._parent.get_name()}});
51+
{% if this.auto_start %}
52+
{{this.get_name()}}.start();
53+
{% endif %}
4854
{% endmacro %}
4955
""")
5056

51-
def __init__(self, **kwargs):
57+
def __init__(self, auto_start=False, **kwargs):
5258
super(LocateControl, self).__init__()
5359
self._name = 'LocateControl'
60+
self.auto_start = auto_start
5461
self.options = parse_options(**kwargs)
5562

5663
def render(self, **kwargs):

0 commit comments

Comments
 (0)