Create maps with arbitrary extents.

100% Map style by Stamen Design, © OpenStreetMap contributors

Map extents can be of any size. They will be subdivided into tiles accordingly.

index.js
import { Vector3, Object3D } from 'three';
import { MapControls } from 'three/examples/jsm/controls/MapControls.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 Helpers from '@giro3d/giro3d/helpers/Helpers.js';
import TiledImageSource from '@giro3d/giro3d/sources/TiledImageSource.js';



// Defines geographic extent: CRS, min/max X, min/max Y
const EPSG3857_BOUNDS = new Extent(
    'EPSG:3857',
    -20037508.342789244, 20037508.342789244,
    -20037508.342789244, 20037508.342789244,
);

let currentMap;

// `viewerDiv` will contain giro3d' rendering area (the canvas element)
const viewerDiv = document.getElementById('viewerDiv');

// Creates a giro3d instance
const instance = new Instance(viewerDiv, {
    crs: EPSG3857_BOUNDS.crs(),
    renderer: {
        clearColor: 0x0a3b59,
    },
});

// Instanciates controls
const controls = new MapControls(instance.camera.camera3D, instance.domElement);
instance.camera.camera3D.position.set(0, 0, 100000000);

instance.useTHREEControls(controls);

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

instance.notifyChange();

const layers = ['stamen_watercolor', 'stamen_toner', 'stamen_terrain'];

let mapCount = 0;

// Create a grid that encompasses the whole EPSG:3857 bounds.
const grid = Helpers.createGrid(new Vector3(0, 0, -10000), EPSG3857_BOUNDS.dimensions().x, 20);
instance.threeObjects.add(grid);

function createMap(extent) {
    if (currentMap) {
        instance.remove(currentMap);
        currentMap = null;
    }

    mapCount++;
    const object3d = new Object3D();
    // Creates a map that will contain the layer
    currentMap = new Map(`${mapCount}`, {
        extent,
        maxSubdivisionLevel: 10,
        object3d,
        showOutline: true,
    });

    currentMap.object3d.position.set(0, 0, mapCount * 10000);

    instance.add(currentMap);

    // Adds an TMS imagery layer
    const layer = layers[mapCount % layers.length];
    currentMap.addLayer(new ColorLayer({
        name: 'osm',
        extent,
        source: new TiledImageSource({ source: new StadiaMaps({ layer, wrapX: false }) }),
    })).catch(e => console.error(e));

    instance.notifyChange();
}

const button = document.getElementById('createMap');

button.onclick = () => {
    const x0 = Math.random();
    const x1 = Math.random();
    const y0 = Math.random();
    const y1 = Math.random();

    const dimensions = EPSG3857_BOUNDS.dimensions();

    const west = EPSG3857_BOUNDS.west() + Math.min(x0, x1) * dimensions.x;
    const east = EPSG3857_BOUNDS.west() + Math.max(x0, x1) * dimensions.x;

    const north = EPSG3857_BOUNDS.south() + Math.max(y0, y1) * dimensions.y;
    const south = EPSG3857_BOUNDS.south() + Math.min(y0, y1) * dimensions.y;

    const extent = new Extent('EPSG:3857', west, east, south, north);

    createMap(extent);
};
index.html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Giro3D - Arbitrary map extents</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="dropdown m-3 position-absolute top-0 end-0">
    <button class="btn btn-primary" id="createMap">Create new map</button>
</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": "arbitrary_map_extents",
  "dependencies": {
    "@giro3d/giro3d": "0.33.1"
  },
  "devDependencies": {
    "vite": "^3.2.3"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}