MarkerCluster and bringToFront / Back

1,066 views
Skip to first unread message

Kelvin Luck

unread,
Nov 27, 2012, 6:22:44 PM11/27/12
to leafl...@googlegroups.com
Hi,

I've built something using Leaflet and MarkerCluster. Thanks so much for an awesome library!

I'm having one problem... I have two separate sets of things on my map, independently clustered. So there are two L.MarkerClusterGroup's added to my map, each with a few hundred markers. I would like to make it so that one set of markers (whether clustered or not) always appears behind the other set of markers (whether that is clustered or not). I have tried calling bringToFront and bringToBack on the different MarkerClusterGroup's but it doesn't seem to help.

Am I missing something obvious or is what I'm trying to do not supported?

Thanks!

Kelvin :)

Paulo Vieira

unread,
Nov 27, 2012, 7:26:42 PM11/27/12
to leafl...@googlegroups.com
I never really used the cluster plugin, but you can always manually set the zIndex of each marker. That is, iterate over all the markers and .setZIndexOffset using some interval for each group (say, between -1000 and 0 for group A, and 1 and 1000 for group B).

Or even better: increment and decrement the zindex of each marker by a large value( markerAZIndex -= 1000, markerBZIndex += 1000). That way you will maintain the relative ordering, which looks nice because it based on the latitude.




Kelvin :)

--
 
 
 

Kelvin Luck

unread,
Nov 28, 2012, 4:47:35 PM11/28/12
to leafl...@googlegroups.com
Thanks for the idea. I just had a chance to try it out and it didn't seem to work :( Some of the clusters will be ordered one way and some will be ordered another way.

I guess I need a way to set the zIndex of the clusters themselves. Is there a way to get a list of all displayed clusters? And to get notified when they are recreated?

Thanks!

Kelvin :)


--
 
 
 

Paulo Vieira

unread,
Nov 28, 2012, 4:59:01 PM11/28/12
to leafl...@googlegroups.com
On Wed, Nov 28, 2012 at 9:47 PM, Kelvin Luck <kel...@kelvinluck.com> wrote:
Thanks for the idea. I just had a chance to try it out and it didn't seem to work :( Some of the clusters will be ordered one way and some will be ordered another way.

I guess I need a way to set the zIndex of the clusters themselves. Is there a way to get a list of all displayed clusters? And to get notified when they are recreated?


I was browsing the new stuff in the master version and just noticed this:

Add setZIndex to LayerGroup

Might be useful to you.

Kelvin Luck

unread,
Nov 28, 2012, 5:24:38 PM11/28/12
to leafl...@googlegroups.com
Thanks - that looks interesting.

I just tried it out and it didn't seem to work though. Although L.MarkerClusterGroup extends L.FeatureGroup (which extends L.LayerGroup), looping over the layers enumerated by eachLayer don't seem to have a setZIndex function... 

Any other ideas?

Kelvin :)


--
 
 
 

Reply all
Reply to author
Forward
0 new messages