Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markercluster doesn't appear in order in the Layers control as an Overlay #422

Closed
DaveF63 opened this issue Nov 18, 2014 · 11 comments
Closed

Comments

@DaveF63
Copy link

DaveF63 commented Nov 18, 2014

Hi
I have various layer groups, but only 'PubsGroup' is created with markercluster. As you can see in the overlays list it's at the bottom, but when I run the code it puts it at the top of the Layer control. If I create Pubsgroup as a standard LayerGroup it remains in its place at the bottom.

Is there solution for this?

Here's snips> of my code:

        var PubsGroup = L.markerClusterGroup({ disableClusteringAtZoom: 18, maxClusterRadius: 40});

        L.geoJson(Pubs,{
            fillColor: "Lightblue ", // style for cafe mapped as polygons
            color: "lightblue",
            weight: 3,
            opacity: 1,
            fillOpacity: 0.2,           

            pointToLayer: function (feature, latlng) {
                if (feature.properties.website !== undefined) {         
                    popupContent = '<a href=' + feature.properties.website + ' target="_blank">' + feature.properties.name + '</a>'
                    } else {popupContent = feature.properties.name}         
                return L.marker(latlng, { // Co-ordinates
                    icon: PubIcon,
                    title: feature.properties.name // Hover over text
                    } 
                ).bindPopup(popupContent)
            }, // pointToLayer
        }).addTo(PubsGroup);
snip>
        //List of all the Overlays layergroups
        var overlays = {
            "Two Tunnels NCN 244": TwoTunnelsGroup,
            "Two Tunnels Circuit": TTCircuitGroup,
            "BikeShops": BikeShopGroup,
            "Cafes":CafesGroup,
            "Parks":ParksGroup,
            "Pubs":PubsGroup,
        };

        // Loads the different base layer maps & overlays 
        L.control.layers(baseLayers,overlays,{autoZIndex:true}).addTo(map);     

        map.addLayer(PubsGroup);    
        map.addLayer(BikeShopGroup);    
        map.addLayer(TTCircuitGroup); // displays two groups (ways & arrows)...
         map.removeLayer(TTCircuitArrowGroup); // but removes arrows as initial zoom is ~17  
@danzel
Copy link
Member

danzel commented Nov 19, 2014

overlays is a map not an array, so the ordering isn't guaranteed.
Search the leaflet issues for something like that, it sounds familiar.

@danzel danzel closed this as completed Nov 19, 2014
@DaveF63
Copy link
Author

DaveF63 commented Nov 19, 2014

Excuse my ignorance, but could you expand on what you mean please? Especially 'overlays is a map not an array'.

I'd already checked the leaflet forum & autoZIndex appears to be working as stated with layergroups.
To clarify, I'm describing how they appear in the layer control dialog (see attached image) not how they appear on the map.

leflet layer control

As I see it (though probably wrong), overlays are a collection of data in layergroups. (From the Leaflet markercluster blog: "...just replace your existing LayerGroup usage with an L.MarkerClusterGroup").

@danzel
Copy link
Member

danzel commented Nov 19, 2014

I mean that the order of items in a {} is not guaranteed. So when you add it to the layers control they can end up in whatever order the JS engine feels like ordering them.

If you want to guarantee ordering I think you need to use these:
http://leafletjs.com/reference.html#control-layers-addbaselayer

@DaveF63
Copy link
Author

DaveF63 commented Nov 21, 2014

OK. I maybe misunderstanding. I thought the autoZIndex:true, option was implemented to overcome the order problem.
I've tried the addBaseLayer/addOverlay options & the order is static apart from markerClusterGroup, which is disappointing. I'm sure I'm not the first ot have come across this problem, so I hope someone can point me in the direction of a solution.

@danzel
Copy link
Member

danzel commented Nov 22, 2014

autoZIndex is just for making the layers on the map have z-index values that line up with their order in the layers list:
http://leafletjs.com/reference.html#control-layers-autozindex

Does the ordering really not work correctly when adding layers one at a time? A jsfiddle to reproduce this would be good.

@DaveF63
Copy link
Author

DaveF63 commented Nov 23, 2014

http://jsfiddle.net/m3vcqxht/2/

The Clustergroup (Pubs) is second in the js code but is first in the control. If Pubs is created with a standard layer group it remains second.

@danzel danzel changed the title Does markercluster obey the autoZIndex of control.layers? Markercluster doesn't appear in order in the Layers control as an Overlay Nov 25, 2014
@danzel danzel reopened this Nov 25, 2014
@danzel
Copy link
Member

danzel commented Nov 25, 2014

Weird. Definitely seems like a bug somewhere, not sure what is broken though!

@ghybs
Copy link
Contributor

ghybs commented Sep 29, 2015

Hi,

Apologies again for commenting an old thread...

Here is the reason for this behaviour:

Fork of DaveF63's last fiddle to highlight the above behaviour: http://jsfiddle.net/Louh70wa/1/

Forcing a predefined list order in Layers Control could be done by stamping the groups in advance on purpose: http://jsfiddle.net/Louh70wa/2/

@ghybs
Copy link
Contributor

ghybs commented Sep 30, 2015

Well, I actually overlooked bullet 2: this ordering still depends on the JavaScript engine good will rather than a spec, so even stamping in advance to try forcing the order may not give the expected result.

Looks like there is no other choice than customizing the Layers Control implementation if we really want to ensure a given order.

@danzel danzel closed this as completed Dec 22, 2015
@danzel
Copy link
Member

danzel commented Dec 22, 2015

Thanks for looking in to this @ghybs

@kirantpatil
Copy link

Hi, how to order the markers, even though I added them serially it is not getting displayed in order.

The markers are not arranged like 1,2,4,5,6,7 but 1,2,7,6,5,4. Please find the below screenshot.

How to resolve it ?

screenshot-2017-12-6 containermon


 L.marker(cont_ll_1, {
                    icon: new L.AwesomeNumberMarkers({
                      number: position2,
                      markerColor: "blue"
                  })}).addTo(markers).bindTooltip("name:" + device.name).openTooltip();

                       L.marker(cont_ll_2, {
                    icon: new L.AwesomeNumberMarkers({
                      number: position1,
                      markerColor: "blue"
                  })}).addTo(markers).bindTooltip("name:" + name1).openTooltip();

                       L.marker(cont_ll_4, {
                    icon: new L.AwesomeNumberMarkers({
                      number: position4,
                      markerColor: "blue"
                  })}).addTo(markers).bindTooltip("name:" + name4).openTooltip();

                       L.marker(cont_ll_5, {
                    icon: new L.AwesomeNumberMarkers({
                      number: position5,
                      markerColor: "blue"
                  })}).addTo(markers).bindTooltip("name:" + name5).openTooltip();


                       L.marker(cont_ll_6, {
                    icon: new L.AwesomeNumberMarkers({
                      number: position6,
                      markerColor: "blue"
                  })}).addTo(markers).bindTooltip("name:" + name6).openTooltip();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants