Move layers up and down in the map.

  • terrain
  • toner
  • watercolor
100% Map style by Stamen Design, © OpenStreetMap contributors

index.js
import StadiaMaps from 'ol/source/StadiaMaps.js';

import Extent from '@giro3d/giro3d/core/geographic/Extent.js';
import Instance from '@giro3d/giro3d/core/Instance.js';
import ColorLayer from '@giro3d/giro3d/core/layer/ColorLayer.js';
import Map from '@giro3d/giro3d/entities/Map.js';
import Inspector from '@giro3d/giro3d/gui/Inspector.js';
import TiledImageSource from '@giro3d/giro3d/sources/TiledImageSource.js';



const crs = 'EPSG:3857';
const mapExtent = Extent.fromCenterAndSize(crs, { x: 256227, y: 5882214 }, 2000000, 2000000);
const viewerDiv = document.getElementById('viewerDiv');

// Creates a Giro3D instance
const instance = new Instance(viewerDiv, { crs });

// Instanciates camera
instance.camera.camera3D.position.set(256227, 5882214, 4000000);

const map = new Map('planar', { extent: mapExtent, backgroundOpacity: 0 });
instance.add(map);

const layerSize = 1000000;

const watercolor = new ColorLayer({
    name: 'watercolor',
    extent: Extent.fromCenterAndSize(crs, { x: -100000, y: 6169226 }, layerSize, layerSize),
    source: new TiledImageSource({
        source: new StadiaMaps({ layer: 'stamen_watercolor', wrapX: false }),
    }),
});

const toner = new ColorLayer({
    name: 'toner',
    extent: Extent.fromCenterAndSize(crs, { x: 500000, y: 5669226 }, layerSize, layerSize),
    source: new TiledImageSource({
        source: new StadiaMaps({ layer: 'stamen_toner', wrapX: false }),
    }),
});

const terrain = new ColorLayer({
    name: 'terrain',
    extent: Extent.fromCenterAndSize(crs, { x: 900000, y: 5169226 }, layerSize, layerSize),
    source: new TiledImageSource({
        source: new StadiaMaps({ layer: 'stamen_terrain', wrapX: false }),
    }),
});

map.addLayer(watercolor);
map.addLayer(toner);
map.addLayer(terrain);

Inspector.attach(document.getElementById('panelDiv'), instance);


const layers = {
    watercolor,
    toner,
    terrain,
};

function bindUI(layer) {
    const id = layer.name;
    const btnUp = document.getElementById(`btn-${id}-up`);
    const btnDown = document.getElementById(`btn-${id}-down`);
    const layerElt = document.getElementById(`${id}`);
    layerElt.layer = layer;
    const container = layerElt.parentNode;

    function reorder() {
        [...container.children]
            .sort((a, b) => (map.getIndex(layers[a.id]) > map.getIndex(layers[b.id]) ? -1 : 1))
            .forEach(node => container.appendChild(node));
    }

    btnUp.onclick = () => {
        map.moveLayerUp(layer);
        reorder();
    };
    btnDown.onclick = () => {
        map.moveLayerDown(layer);
        reorder();
    };

    reorder();
}

bindUI(watercolor);
bindUI(toner);
bindUI(terrain);
index.html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Giro3D - Change layer order</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
  <style>
    body {
      padding: 0;
      margin: 0;
      width: 100vw;
      height: 100vh;
    }

    #viewerDiv {
      width: 100%;
      height: 100%;
    }

    #panelDiv {
      position: absolute;
      top: 0;
      left: 0;
    }
    
  </style>
</head>

<body>
  <div id="viewerDiv"></div>
  <div id="panelDiv"></div>
  <div class="m-2 position-absolute top-0 end-0">
    <div class="m-1">
        <ul id="layer-list" class="list-group">
            <li id="terrain" class="list-group-item">
                <div class="float-start align-start mx-4">terrain</div>
                <div
                    class="btn-group btn-group-sm float-end"
                    role="group"
                    aria-label="Basic example"
                >
                    <button id="btn-terrain-up" type="button" class="btn btn-primary">Up</button>
                    <button id="btn-terrain-down" type="button" class="btn btn-primary">
                        Down
                    </button>
                </div>
            </li>

            <li id="toner" class="list-group-item">
                <div class="float-start align-start mx-4">toner</div>
                <div
                    class="btn-group btn-group-sm float-end"
                    role="group"
                    aria-label="Basic example"
                >
                    <button id="btn-toner-up" type="button" class="btn btn-primary">Up</button>
                    <button id="btn-toner-down" type="button" class="btn btn-primary">Down</button>
                </div>
            </li>

            <li id="watercolor" class="list-group-item">
                <div class="float-start align-start mx-4">watercolor</div>
                <div
                    class="btn-group btn-group-sm float-end"
                    role="group"
                    aria-label="Basic example"
                >
                    <button id="btn-watercolor-up" type="button" class="btn btn-primary">Up</button>
                    <button id="btn-watercolor-down" type="button" class="btn btn-primary">
                        Down
                    </button>
                </div>
            </li>
        </ul>
    </div>
</div>

  <script type="module" src="index.js"></script>
  <script>
    /* activate popovers */
    const popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'));
    popoverTriggerList.map(
      // bootstrap is used as script in the template, disable warning about undef
      // eslint-disable-next-line no-undef
      popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl, {
        trigger: 'hover',
        placement: 'left',
        content: document.getElementById(popoverTriggerEl.getAttribute('data-bs-content')).innerHTML,
        html: true,
      }),
    );
  </script>
</body>

</html>
package.json
{
  "name": "layer_ordering",
  "dependencies": {
    "@giro3d/giro3d": "0.36.0",
    "colormap": "^2.3.2",
    "@turf/turf": "^6.5.0"
  },
  "devDependencies": {
    "vite": "^3.2.3"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}