question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Idea: LayerGroups show/hide via grouped css display:none/block

See original GitHub issue

I have an idea for how Leaflet could possibly be improved in order to increase performance for quickly showing/hiding LayerGroups:

I’ve had a hard time determining the best way to turn on/off LayerGroups, for maximizing speed and performance. In my mapping application, I have various markers on the map showing or hidden, depending on what zoom level the map is currently at. I simply hook the zoomend event and, based off of predefined values, LayerGroups are either added to the map or removed from the map, depending on if they should be visible or not for the current zoom level.

So the problem with this, is that myLayerGroup.addTo(map) and map.removeLayer(myLayerGroup), basically consists of Leaflet iterating through each layer in the group and individually adding to or removing from the map. I feel like this is potentially slow, especially if you have a lot of markers on a map.

So I had a bit of an idea today. Lets say I have 5 LayerGroups of markers. Each one of those markers is put in this DOM heirarchy:

<div class="leaflet-marker-pane">
    <div class="leaflet-marker-icon leaflet-zoom-animated"></div>
    <div class="leaflet-marker-icon leaflet-zoom-animated"></div>
    <div class="leaflet-marker-icon leaflet-zoom-animated"></div>
    <div class="leaflet-marker-icon leaflet-zoom-animated"></div>
    etc...
</div>

So all of the markers are put under the same div.leaflet-marker-pane, even though they are in different LayerGroups. So here is where my idea comes into play: What would happen if you grouped Layers under individual div’s, one for each LayerGroup? So you would have this:

<div class="leaflet-marker-pane">
    <div class='myLayerGroupContainer'>
        <div class="leaflet-marker-icon leaflet-zoom-animated"></div>
        <div class="leaflet-marker-icon leaflet-zoom-animated"></div>
    </div>
    <div class='someOtherLayerGroupContainer'>
        <div class="leaflet-marker-icon leaflet-zoom-animated"></div>
        <div class="leaflet-marker-icon leaflet-zoom-animated"></div>
    </div>
    etc...
</div>

So now, if someone wanted to show/hide a LayerGroup, it would simply be a matter of doing myLayerGroup.hide() or myLayerGroup.show(), which would simply change the css display to none or block. Now it’s just a basic css change to hide/show all the LayerGroup markers. No need to iterate through 100’s (or 1000’s) of individual markers/layers and adding/removing them. Seems like this would be MUCH faster. No more JavaScript overhead.

Also another advantage to this approach, is that if your LayerGroup consists of multiple types of layers (markers, polygons, popups, etc), each type of layer could still be grouped under a <div class='myLayerGroupContainer'> within each appropriate map pane. So the usage would be the same.

That being said, I don’t really know anything about the performance drawbacks of having a bunch of layers/markers on a map but simply having them hidden. I know that removing a LayerGroup actually removed the layers from the DOM. So if there were 1000’s of markers on the map, but simply hidden in the DOM, does it affect performance or not? I don’t really know the answer to this, but in this type of situation, map.remove(myLayerGroup) and myLayerGroup.addTo(map) is still an option.

What do you guys think about this? Would this be worth investigating? I haven’t tested this yet but it seems like it would be a great performance improvement if you have a mapping application that shows/hides markers a lot.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
thewebkidcommented, Aug 17, 2017

Ah thanks for telling me about panes. That’ll probably save me a lot of “pain”. Will check it out.

1reaction
Jakobudcommented, Jun 5, 2013

I was thinking about the best way to implement this in the code last night. Right now, map.panes consists of divs that are created with DomUtil. Maybe changes panes so that they are objects rather than DOM elements. This would allow you to add methods like add(layer) and remove(layer) instead of appendChild() and removeChild(). The advantage of this, is that you could allow an optional second argument to add() in order to place a new layer DOM element within a sub-div of the pane:

panes.markerPane.add(this._icon);
// results in:
<div class="leaflet-marker-pane">
    <div class="leaflet-marker-icon leaflet-zoom-animated"></div>
</div>

and

panes.markerPane.add(this._icon, 'someContainerName');
// results in:
<div class="leaflet-marker-pane">
    <div class='someContainerName'>
        <div class="leaflet-marker-icon leaflet-zoom-animated"></div>
    </div>
</div>

I’m not familiar enough with the src to know if that’s a good approach or not. It might not be a good approach because it might force any 3rd party layer plugins to change their code in order to support these LayerGroup divs.

Really looking forward to this feature being added!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Places it's tempting to use `display: none;`, but don't | CSS-Tricks
You want to hide something on a page, so: .hide { display: none; }. But wait! By applying that class to an element...
Read more >
Is there a shortcut to toggle "display: none/block" in element's ...
We can hide the element by pressing some number of keys. You can do this in this way.
Read more >
CSS Display Module Level 3 - W3C
Abstract. This module describes how the CSS formatting box tree is generated from the document element tree and defines the display property ...
Read more >
Block formatting context - Developer guides - MDN Web Docs
A block formatting context (BFC) is a part of a visual CSS rendering of a web page. It's the region in which the...
Read more >
CSS Layout - The display Property - W3Schools
The display property specifies if/how an element is displayed. Every HTML element has a default display value depending on what type of element...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found