Leaflet Z-index
I'm porting some Google Maps code to Leaflet (well, Mapbox actually). I have quite a lot of shapes (like rectangles, polygons) and markers on the map and I need the ability to adju
Solution 1:
Currently it's not available in the Leaflet API. Luckily if Leaflet is using SVG, all objects are DOM elements and we can simply change their order. Here's a sample code:
L.Path.prototype.setZIndex = function (index)
{
var obj = $(this._container || this._path);
if (!obj.length) return; // not supported on canvasvar parent = obj.parent();
obj.data('order', index).detach();
var lower = parent.children().filter(function ()
{
var order = $(this).data('order');
if (order == undefined) returnfalse;
return order <= index;
});
if (lower.length)
{
lower.last().after(obj);
}
else
{
parent.prepend(obj);
}
};
Solution 2:
Leaflet by default puts shapes ("overlays") and markers in different panes, with different z-index
values:
Solution 3:
Have a look at zIndexOffset
, an option from the Marker class.
You can bind a function to the layeradd
event to style each marker individually.
myLayer.on('layeradd', function (e) {
var marker = e.layer;
var zindex = 0;
var feature = marker.feature;
if (feature.geometry.type == 'Point') {
marker.setStyle({
'zIndexOffset': 3
}
});
});
Post a Comment for "Leaflet Z-index"