diff --git a/folium/plugins/locate_control.py b/folium/plugins/locate_control.py index f45789415a..1a19bade91 100644 --- a/folium/plugins/locate_control.py +++ b/folium/plugins/locate_control.py @@ -26,6 +26,8 @@ class LocateControl(JSCSSMixin, MacroElement): Once user location is founded, the map will automatically centered in using user coordinates. **kwargs For possible options, see https://github.com/domoritz/leaflet-locatecontrol + Options can be given in snake_case or lowerCamelCase, for example + ``fly_to=True`` or ``flyTo=True``. Examples -------- @@ -47,7 +49,7 @@ class LocateControl(JSCSSMixin, MacroElement): _template = Template(""" {% macro script(this, kwargs) %} var {{this.get_name()}} = L.control.locate( - {{this.options | tojson}} + {{this.options | tojavascript}} ).addTo({{this._parent.get_name()}}); {% if this.auto_start %} {{this.get_name()}}.start(); diff --git a/tests/plugins/test_locate_control.py b/tests/plugins/test_locate_control.py new file mode 100644 index 0000000000..1b9cda6816 --- /dev/null +++ b/tests/plugins/test_locate_control.py @@ -0,0 +1,76 @@ +""" +Test LocateControl +------------------ + +""" + +import folium +from folium import plugins +from folium.template import Template +from folium.utilities import JsCode, normalize + + +def test_locate_control(): + m = folium.Map([47, 3], zoom_start=1) + lc = plugins.LocateControl().add_to(m) + + out = normalize(m._parent.render()) + + tmpl = Template(""" + var {{this.get_name()}} = L.control.locate( + {{this.options | tojavascript}} + ).addTo({{this._parent.get_name()}}); + """) + assert normalize(tmpl.render(this=lc)) in out + assert f"{lc.get_name()}.start();" not in out + + +def test_locate_control_auto_start(): + m = folium.Map() + lc = plugins.LocateControl(auto_start=True).add_to(m) + + out = normalize(m._parent.render()) + + assert f"{lc.get_name()}.start();" in out + + +def test_locate_control_snake_case_options(): + m = folium.Map() + plugins.LocateControl( + fly_to=True, + keep_current_zoom_level=True, + locate_options={"enable_high_accuracy": True}, + ).add_to(m) + + out = normalize(m._parent.render()) + + assert '"flyTo": true' in out + assert '"keepCurrentZoomLevel": true' in out + assert '"enableHighAccuracy": true' in out + assert "fly_to" not in out + assert "keep_current_zoom_level" not in out + + +def test_locate_control_camel_case_options_unchanged(): + m = folium.Map() + plugins.LocateControl( + flyTo=True, + strings={"title": "Show me where I am", "popup": "You are here"}, + ).add_to(m) + + out = normalize(m._parent.render()) + + assert '"flyTo": true' in out + assert '"title": "Show me where I am"' in out + assert '"popup": "You are here"' in out + + +def test_locate_control_js_code_option(): + m = folium.Map() + plugins.LocateControl( + on_location_error=JsCode("function(err) { console.log(err.message); }"), + ).add_to(m) + + out = normalize(m._parent.render()) + + assert '"onLocationError": function(err) { console.log(err.message); }' in out