From 382a6473100a2be3989a72955932792910aaa95e Mon Sep 17 00:00:00 2001 From: fullonic Date: Tue, 2 Apr 2019 15:52:47 +0200 Subject: [PATCH 1/4] add user location based on leaflet.locate() --- folium/plugins/__init__.py | 4 +- folium/plugins/locate.py | 86 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100755 folium/plugins/locate.py diff --git a/folium/plugins/__init__.py b/folium/plugins/__init__.py index 2a61ad560c..a31ebf935e 100644 --- a/folium/plugins/__init__.py +++ b/folium/plugins/__init__.py @@ -21,6 +21,7 @@ from folium.plugins.fullscreen import Fullscreen from folium.plugins.heat_map import HeatMap from folium.plugins.heat_map_withtime import HeatMapWithTime +from folium.plugins.locate import Locate from folium.plugins.marker_cluster import MarkerCluster from folium.plugins.measure_control import MeasureControl from folium.plugins.minimap import MiniMap @@ -47,6 +48,7 @@ 'Fullscreen', 'HeatMap', 'HeatMapWithTime', + 'Locate', 'MarkerCluster', 'MeasureControl', 'MiniMap', @@ -59,4 +61,4 @@ 'TimeSliderChoropleth', 'TimestampedGeoJson', 'TimestampedWmsTileLayers', - ] +] diff --git a/folium/plugins/locate.py b/folium/plugins/locate.py new file mode 100755 index 0000000000..a8c0cf84f0 --- /dev/null +++ b/folium/plugins/locate.py @@ -0,0 +1,86 @@ +# -*- coding: utf-8 -*- + +from __future__ import (absolute_import, division, print_function) + +from branca.element import Figure, MacroElement +from jinja2 import Template +from folium import Popup + + +class Locate(MacroElement): + """ + Fontawesome icons: https://fontawesome.com/v4.7.0/icons/. + + https://leafletjs.com/reference-1.4.0.html#locate-options + + Parameters + ---------- + icon: Icon plugin + the Icon plugin to use to render the marker. + popup: string, default: "Your current location" + Label for the Marker; only strings are valid. + show: bool, default True + True renders the popup open on page load. + + Examples + -------- + >>> import folium + >>> from folium.plugins import Locate + >>> map = folium.Map() + # with default values + >>> Locate().add_to(map) + # with custom values + >>>Locate(icon=folium.Icon(icon="fa-ship", prefix='fa'), + popup="Sua actual localização", show=False).add_to(map) + """ + + _template = Template(u""" + {% macro script(this, kwargs) %} + function onLocationFound(e) { + var {{this.get_name()}} = L.marker(e.latlng).addTo({{this._parent.get_name()}}) + .bindPopup("{{this.popup}}"){% if this.show%}.openPopup(){% endif %}; + {% if this.icon != None %}{{this.icon}}{% endif %} + } + function onLocationError(e) { + alert(e.message); + } + {{this._parent.get_name()}}.on( + 'locationfound', onLocationFound); + {{this._parent.get_name()}}.on( + 'locationerror', onLocationError); + + {{this._parent.get_name()}}.locate( + {setView: true, maxZoom: 16}) + {% endmacro %} + """) + + def __init__(self, icon=None, popup=None, show=True): + """Initialization.""" + super(Locate, self).__init__() + self._name = 'Locate' + self.show = show + if icon: + self.icon = self.custom_icon(icon) + + if popup is None: + self.popup = "Your current location" + elif not isinstance(popup, Popup): + self.popup = popup + else: + raise TypeError("Popup can only be a string, not a folium.Popup") + + def custom_icon(self, icon): + """Create a custom icon from folium.Icon. + + This method generates a figure transforming the folium.Icon in plain javascript in order to + be able to place the new icon inside the function onLocationFound(). + """ + figure = Figure() + figure.script.add_child(icon) + # Iterates over the figure.script.render: First render returns None and the second the Icon + for i in range(2): + _string_icon = figure.script.render() + # Gets current icon parent name. At the moment the parent name is folium.Map + _icon = icon._parent.get_name() + # Replace parent name to the self making possible change the default icon + return _string_icon.replace(_icon, self.get_name()) From a596695c0ee2987e4f31398667426dd34e2e96f0 Mon Sep 17 00:00:00 2001 From: fullonic Date: Tue, 2 Apr 2019 18:31:27 +0200 Subject: [PATCH 2/4] Add Locate control plugin --- folium/plugins/__init__.py | 2 ++ folium/plugins/locate_control.py | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 folium/plugins/locate_control.py diff --git a/folium/plugins/__init__.py b/folium/plugins/__init__.py index a31ebf935e..650631cd38 100644 --- a/folium/plugins/__init__.py +++ b/folium/plugins/__init__.py @@ -22,6 +22,7 @@ from folium.plugins.heat_map import HeatMap from folium.plugins.heat_map_withtime import HeatMapWithTime from folium.plugins.locate import Locate +from folium.plugins.locate_control import LocateControl from folium.plugins.marker_cluster import MarkerCluster from folium.plugins.measure_control import MeasureControl from folium.plugins.minimap import MiniMap @@ -49,6 +50,7 @@ 'HeatMap', 'HeatMapWithTime', 'Locate', + 'LocateControl', 'MarkerCluster', 'MeasureControl', 'MiniMap', diff --git a/folium/plugins/locate_control.py b/folium/plugins/locate_control.py new file mode 100644 index 0000000000..7ed4474564 --- /dev/null +++ b/folium/plugins/locate_control.py @@ -0,0 +1,39 @@ +"""Add Locate control to folium Map. + +Based on leaflet plugin: https://github.com/domoritz/leaflet-locatecontrol +""" + +from __future__ import (absolute_import, division, print_function) +from branca.element import MacroElement, Figure, JavascriptLink, CssLink +from jinja2 import Template + + +class LocateControl(MacroElement): + """LocateControl.""" + + _template = Template(u""" + {% macro script(this, kwargs) %} + var {{this.get_name()}} = L.control.locate({ + strings: { + title: "Show me where I am, yo!" + } + }).addTo({{this._parent.get_name()}}); + {% endmacro %} + """) + + def __init__(self): + """Initialization.""" + super(LocateControl, self).__init__() + self._name = "LocateControl" + + def render(self, **kwargs): + super(LocateControl, self).render(**kwargs) + figure = self.get_root() + assert isinstance(figure, Figure), ('You cannot render this Element ' + 'if it is not in a Figure.') + + figure.header.add_child( + CssLink( + "https://cdnjs.cloudflare.com/ajax/libs/leaflet-locatecontrol/0.66.2/L.Control.Locate.min.css")) # noqa + figure.header.add_child(JavascriptLink( + "https://cdnjs.cloudflare.com/ajax/libs/leaflet-locatecontrol/0.66.2/L.Control.Locate.min.js")) # noqa From bd4ac9fe1be8e917414adb11ce02f07c2eb3af35 Mon Sep 17 00:00:00 2001 From: fullonic Date: Tue, 2 Apr 2019 19:43:48 +0200 Subject: [PATCH 3/4] Cleanup master --- folium/plugins/__init__.py | 4 -- folium/plugins/locate.py | 86 -------------------------------- folium/plugins/locate_control.py | 39 --------------- 3 files changed, 129 deletions(-) delete mode 100755 folium/plugins/locate.py delete mode 100644 folium/plugins/locate_control.py diff --git a/folium/plugins/__init__.py b/folium/plugins/__init__.py index 942c75ab6b..38f63d6c59 100644 --- a/folium/plugins/__init__.py +++ b/folium/plugins/__init__.py @@ -19,8 +19,6 @@ from folium.plugins.fullscreen import Fullscreen from folium.plugins.heat_map import HeatMap from folium.plugins.heat_map_withtime import HeatMapWithTime -from folium.plugins.locate import Locate -from folium.plugins.locate_control import LocateControl from folium.plugins.marker_cluster import MarkerCluster from folium.plugins.measure_control import MeasureControl from folium.plugins.minimap import MiniMap @@ -47,8 +45,6 @@ 'Fullscreen', 'HeatMap', 'HeatMapWithTime', - 'Locate', - 'LocateControl', 'MarkerCluster', 'MeasureControl', 'MiniMap', diff --git a/folium/plugins/locate.py b/folium/plugins/locate.py deleted file mode 100755 index a8c0cf84f0..0000000000 --- a/folium/plugins/locate.py +++ /dev/null @@ -1,86 +0,0 @@ -# -*- coding: utf-8 -*- - -from __future__ import (absolute_import, division, print_function) - -from branca.element import Figure, MacroElement -from jinja2 import Template -from folium import Popup - - -class Locate(MacroElement): - """ - Fontawesome icons: https://fontawesome.com/v4.7.0/icons/. - - https://leafletjs.com/reference-1.4.0.html#locate-options - - Parameters - ---------- - icon: Icon plugin - the Icon plugin to use to render the marker. - popup: string, default: "Your current location" - Label for the Marker; only strings are valid. - show: bool, default True - True renders the popup open on page load. - - Examples - -------- - >>> import folium - >>> from folium.plugins import Locate - >>> map = folium.Map() - # with default values - >>> Locate().add_to(map) - # with custom values - >>>Locate(icon=folium.Icon(icon="fa-ship", prefix='fa'), - popup="Sua actual localização", show=False).add_to(map) - """ - - _template = Template(u""" - {% macro script(this, kwargs) %} - function onLocationFound(e) { - var {{this.get_name()}} = L.marker(e.latlng).addTo({{this._parent.get_name()}}) - .bindPopup("{{this.popup}}"){% if this.show%}.openPopup(){% endif %}; - {% if this.icon != None %}{{this.icon}}{% endif %} - } - function onLocationError(e) { - alert(e.message); - } - {{this._parent.get_name()}}.on( - 'locationfound', onLocationFound); - {{this._parent.get_name()}}.on( - 'locationerror', onLocationError); - - {{this._parent.get_name()}}.locate( - {setView: true, maxZoom: 16}) - {% endmacro %} - """) - - def __init__(self, icon=None, popup=None, show=True): - """Initialization.""" - super(Locate, self).__init__() - self._name = 'Locate' - self.show = show - if icon: - self.icon = self.custom_icon(icon) - - if popup is None: - self.popup = "Your current location" - elif not isinstance(popup, Popup): - self.popup = popup - else: - raise TypeError("Popup can only be a string, not a folium.Popup") - - def custom_icon(self, icon): - """Create a custom icon from folium.Icon. - - This method generates a figure transforming the folium.Icon in plain javascript in order to - be able to place the new icon inside the function onLocationFound(). - """ - figure = Figure() - figure.script.add_child(icon) - # Iterates over the figure.script.render: First render returns None and the second the Icon - for i in range(2): - _string_icon = figure.script.render() - # Gets current icon parent name. At the moment the parent name is folium.Map - _icon = icon._parent.get_name() - # Replace parent name to the self making possible change the default icon - return _string_icon.replace(_icon, self.get_name()) diff --git a/folium/plugins/locate_control.py b/folium/plugins/locate_control.py deleted file mode 100644 index 7ed4474564..0000000000 --- a/folium/plugins/locate_control.py +++ /dev/null @@ -1,39 +0,0 @@ -"""Add Locate control to folium Map. - -Based on leaflet plugin: https://github.com/domoritz/leaflet-locatecontrol -""" - -from __future__ import (absolute_import, division, print_function) -from branca.element import MacroElement, Figure, JavascriptLink, CssLink -from jinja2 import Template - - -class LocateControl(MacroElement): - """LocateControl.""" - - _template = Template(u""" - {% macro script(this, kwargs) %} - var {{this.get_name()}} = L.control.locate({ - strings: { - title: "Show me where I am, yo!" - } - }).addTo({{this._parent.get_name()}}); - {% endmacro %} - """) - - def __init__(self): - """Initialization.""" - super(LocateControl, self).__init__() - self._name = "LocateControl" - - def render(self, **kwargs): - super(LocateControl, self).render(**kwargs) - figure = self.get_root() - assert isinstance(figure, Figure), ('You cannot render this Element ' - 'if it is not in a Figure.') - - figure.header.add_child( - CssLink( - "https://cdnjs.cloudflare.com/ajax/libs/leaflet-locatecontrol/0.66.2/L.Control.Locate.min.css")) # noqa - figure.header.add_child(JavascriptLink( - "https://cdnjs.cloudflare.com/ajax/libs/leaflet-locatecontrol/0.66.2/L.Control.Locate.min.js")) # noqa From 5928b586ad00bb1254041716b77c012f73339328 Mon Sep 17 00:00:00 2001 From: fullonic Date: Fri, 3 May 2019 23:14:12 +0200 Subject: [PATCH 4/4] fix #1129 --- folium/features.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/folium/features.py b/folium/features.py index 93d65616c5..8dd8c4ca1f 100644 --- a/folium/features.py +++ b/folium/features.py @@ -424,10 +424,16 @@ class GeoJson(Layer): }).addTo({{ this._parent.get_name() }}); {%- if this.embed %} {{ this.get_name() }}.addData({{ this.data|tojson }}); + {% if this._parent.get_name()|truncate(14, True, "") == "marker_cluster" %} + {{ this.get_name() }}.addTo({{ this._parent.get_name() }}); + {% endif %} {%- else %} $.ajax({url: {{ this.embed_link|tojson }}, dataType: 'json', async: true, success: function(data) { {{ this.get_name() }}.addData(data); + {% if (this._parent.get_name()|truncate(14, True, "") == "marker_cluster") %} + {{ this.get_name() }}.addTo({{ this._parent.get_name() }}); + {% endif %} }}); {%- endif %} {% endmacro %}