Saturday, 5 March 2016

Changing GeoJSON feature draw order with Leaflet


I have county polygons in GeoJSON format. Obviously, some of the counties share an edge.


When a county is hovered over, the stroke and fill change to a red color.


The problem is that the stroke of some of the polygons is covered by the one next to it.


Is there a way to reorder the features so that the feature that is hovered over moves to the top?



I've seen that there's a bringToFront method, but that is for a layer, not a feature.


Any ideas?



Answer



You should really use the bringToFront method.


In Leaflet, everything is a layer (except controls), even when it is the representation of a GeoJSON feature. In the latter case, we are even talking about vector layers.


The bringToFront and bringToBack methods are specific to vector layers, so that you can re-order them within their SVG or Canvas container (whereas raster and HTML layers can get a CSS class with z-index for that purpose).


Demo: https://jsfiddle.net/3v7hd2vx/228/


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...