Skip to content

Popups get hidden below overlay layers by LayerControl #826

Closed

Description

@mjgleason

If you add popups to a map along with an overlay layer, controlled with the LayerControl, the popups will get buried when the overlay layer is turned off and back on again. They are still there, but are now behind the overlay and therefore don't respond as long as the overlay layers is visible.

I believe that simply changing the Template for Popups (https://github.com/python-visualization/folium/blob/master/folium/map.py#L300-L313) to the code below should fix this:

(u"""
            var {{this.get_name()}} = L.popup({maxWidth: '{{this.max_width}}'});

            {% for name, element in this.html._children.items() %}
                var {{name}} = $('{{element.render(**kwargs).replace('\\n',' ')}}')[0];
                {{this.get_name()}}.setContent({{name}});

            {% endfor %}

            {{this._parent.get_name()}}.bindPopup({{this.get_name()}});

            {{this._parent._parent.get_name()}}.on('overlayadd', function(){
                {{this._parent.get_name()}}.bringToFront();
            });

            {% for name, element in this.script._children.items() %}
                {{element.render()}}

            {% endfor %}
            """)

Activity

  1. Conengmo commented on Mar 26, 2018

    @Conengmo
    Member

    I'm unable to reproduce your problem. Could you provide a code snippit that produces it? Also, which version are you on, 0.5.0 or the Github master?

    I tried this, but the popup stays above the overlay:

    m = folium.Map()
    folium.Marker((0, 0), popup=folium.Popup('hello')).add_to(m)
    folium.TileLayer(tiles='cartodbpositron', overlay=True).add_to(m)
    folium.LayerControl(collapsed=False).add_to(m)
    m.save('map.html')
    
  2. mjgleason commented on Mar 27, 2018

    @mjgleason
    Author

    @Conengmo I'm on 0.5.0. Here's a snippet that should reproduce the problem:

    import folium
    import json
    
    # create polygon geojson
    g = {'crs': {u'properties': {u'name': u'urn:ogc:def:crs:OGC:1.3:CRS84'}, 'type': 'name'},
         'features': [{'geometry': { "type": "Polygon", "coordinates": [[[-1,1], [-1,-1], [1,-1], [1,1], [-1,1]]],
                                    'properties': {'id': 1}}, 
                                    'type': 'Feature'}],
         'type': u'FeatureCollection'}
    gj = json.dumps(g)
    
    
    m = folium.Map()
    # add polygon overlay
    folium.GeoJson(gj, overlay=True, name="Test Layer").add_to(m)
    # add polygon marker popup with the same geometry
    marker = folium.features.PolygonMarker([[[-1,1], [-1,-1], [1,-1], [1,1], [-1,1]]], color='white', weight=0, fill_color='white', fill_opacity=0, popup="Hello!")
    marker.add_to(m)
    # add layer control
    folium.LayerControl().add_to(m)
    # display map
    m
  3. Conengmo commented on Mar 27, 2018

    @Conengmo
    Member

    That snippet indeed reproduced the problem. So we got two overlays, a geojson and a polygon, and when the geojson layer is untoggled and toggled again, it moves in front of the polygon. And what you want is the polygon to stay in front. It's not really about the popups, but about the polygon.

    I don't think your solution is the right fix. Firstly it only works if the polygon is directly added to the map, and not to another layer (but that can be fixed). More importantly, whenever an overlay is toggled, layers with popups get put on front. I suppose that shouldn't be the default behavior, because then other users who want another ordering get in trouble.

    An easy solution is to add the polygon to a FeatureGroup, so you can also toggle it in LayerControl. This would require you toggling the group to put it in front again after it got to the back. We don't have to change anything to folium for this. Like this:

    fg = folium.FeatureGroup(name='polygon').add_to(m)
    marker = folium.Polygon([[[-1,1], [-1,-1], [1,-1], [1,1], [-1,1]]], color='white', weight=0, fill_color='white', fill_opacity=0, popup="Hello!")
    marker.add_to(fg)
    

    You do have a point that the overlayadd event and bringToFront method can be useful. What about adding an option to folium to define which layers should always stay in front? To try this out I added it to the Map class in my own branch: https://github.com/Conengmo/folium/tree/keep-in-front. You can try it out with this code snippet:

    m = folium.Map((0, 0), zoom_start=8)
    coords = [[[-1, 1], [-1, -1], [1, -1], [1, 1], [-1, 1]]]
    fg_bl = folium.FeatureGroup(name='blue').add_to(m)
    folium.Polygon(coords, color='blue', fill_color='blue', popup="Blue!").add_to(fg_bl)
    fg_gr = folium.FeatureGroup(name='green').add_to(m)
    folium.Polygon(coords, color='green', fill_color='green', popup="Green!").add_to(fg_gr)
    marker = folium.Polygon(coords, color='red', fill_color='red', popup="Red!")
    marker.add_to(m)
    m.keep_in_front(fg_bl)
    folium.LayerControl(collapsed=False).add_to(m)
    m.save('_map.html')
    

    At load the red marker is in front, because it got added to the map last. When one of the two overlays is untoggled, nothing changes. But when the untoggled overlay is toggled again, the blue marker gets in front and stays there.
    If you do m.keep_in_front(marker), the red marker starts and stays in front.

  4. mjgleason commented on Mar 28, 2018

    @mjgleason
    Author

    @Conengmo I defer to your judgement completely on this. Your logic sounds right -- my code fixed my particular use case but isn't general purpose enough. Just wanted to raise this issue as a potential limitation of the current capabilities.

  5. added
    enhancement
    work in progress
    and removed
    enhancement
    work in progress
    on Apr 21, 2018
  6. ocefpaf commented on May 7, 2018

    @ocefpaf
    Member

    Close in #840

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions