diff --git a/folium/folium.py b/folium/folium.py index 377d2bd0ff..d810f4a99e 100644 --- a/folium/folium.py +++ b/folium/folium.py @@ -169,28 +169,27 @@ class Map(MacroElement): _template = Template(u""" {% macro header(this, kwargs) %} {% endmacro %} +{% macro html(this, kwargs) %} +
+{% endmacro %} +{% macro script(this, kwargs) %} + {% if this.max_bounds %} + var southWest = L.latLng({{ this.min_lat }}, {{ this.min_lon }}); + var northEast = L.latLng({{ this.max_lat }}, {{ this.max_lon }}); + var bounds = L.latLngBounds(southWest, northEast); + {% else %} + var bounds = null; + {% endif %} -{% macro html(this, kwargs) -%} - -{%- endmacro %} - -{% macro script(this, kwargs) -%} -{% if this.max_bounds -%} - var southWest = L.latLng({{ this.min_lat }}, {{ this.min_lon }}); - var northEast = L.latLng({{ this.max_lat }}, {{ this.max_lon }}); - var bounds = L.latLngBounds(southWest, northEast); -{% else %} - var bounds = null; -{%- endif %} var {{this.get_name()}} = L.map( '{{this.get_name()}}', { center: [{{this.location[0]}}, {{this.location[1]}}], @@ -201,7 +200,18 @@ class Map(MacroElement): crs: L.CRS.{{this.crs}} }); {% if this.control_scale %}L.control.scale().addTo({{this.get_name()}});{% endif %} -{% endmacro -%} + + {% if this.objects_to_stay_in_front %} + function objects_in_front() { + {% for obj in this.objects_to_stay_in_front %} + {{ obj.get_name() }}.bringToFront(); + {% endfor %} + }; + +{{ this.get_name() }}.on("overlayadd", objects_in_front); +$(document).ready(objects_in_front); +{% endif %} +{% endmacro %} """) # noqa def __init__(self, location=None, width='100%', height='100%', @@ -254,6 +264,8 @@ def __init__(self, location=None, width='100%', height='100%', disable_3d ) + self.objects_to_stay_in_front = [] + if tiles: self.add_tile_layer( tiles=tiles, min_zoom=min_zoom, max_zoom=max_zoom, @@ -604,3 +616,17 @@ def highlight_function(x): caption=legend_name, ) self.add_child(color_scale) + + def keep_in_front(self, *args): + """Pass one or multiples object that must stay in front. + + The ordering matters, the last one is put on top. + + Parameters + ---------- + *args : + Variable length argument list. Any folium object that counts as an + overlay. For example FeatureGroup or a vector object such as Marker. + """ + for obj in args: + self.objects_to_stay_in_front.append(obj)