set the zIndex layer order for geoJson layers? - Javascript Leaflet

Javascript examples for Leaflet:Layer

Description

set the zIndex layer order for geoJson layers?

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://npmcdn.com/leaflet@1.0.0-rc.3/dist/leaflet-src.js"></script> 
      <link rel="stylesheet" type="text/css" href="https://unpkg.com/leaflet@1.0.0/dist/leaflet.css"> 
      <script type="text/javascript" src="https://unpkg.com/leaflet@1.0.0/dist/leaflet.js"></script> 
      <style id="compiled-css" type="text/css">

#map {/* ww  w  . j a v a 2s.c  o  m*/
   height: 500px;
}


      </style> 
      <script type="text/javascript">
    window.onload=function(){
var map = L.map("map");
L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png").addTo(map);
map.setView([48.85, 2.35], 12);
map.createPane("pane250").style.zIndex = 250; // between tiles and overlays
map.createPane("pane450").style.zIndex = 450; // between overlays and shadows
map.createPane("pane620").style.zIndex = 620; // between markers and tooltips
map.createPane("pane800").style.zIndex = 800; // above popups
var geojsonData = L.layerGroup([
   L.rectangle([[48.84, 2.33], [48.86, 2.37]]),
  L.marker([48.84, 2.33]),
  L.marker([48.835, 2.35])
]).toGeoJSON();
var newGroup = L.layerGroup();
L.geoJSON(geojsonData, {
   onEachFeature: function (feature, layer) {
     switch (feature.geometry.type) {
       case "Polygon":
         newGroup.addLayer(L.geoJSON(feature, {
           pane: "pane450",
          style: { fillOpacity: 0.8 }
        }).bindPopup("polygon in pane450"));
        break;
      case "Point":
        newGroup.addLayer(L.marker(layer.getLatLng(), { pane: "pane250" }).bindPopup("marker in pane250"));
        break;
      default: // Add the layer as-is
        newGroup.addLayer(layer);
    }
  }
});
newGroup.addTo(map);
L.rectangle([[48.83, 2.34], [48.87, 2.36]], {
   color: "red"
}).bindPopup("Red rectangle in normal overlay pane (z-index 400)").addTo(map);
L.marker([48.855, 2.35]).bindPopup("Marker in normal markers pane (z-index 600)").addTo(map);
    }

      </script> 
   </head> 
   <body> 
      <div id="map"></div>  
   </body>
</html>

Related Tutorials