Create maps with arbitrary extents.
Map extents can be of any size. They will be subdivided into tiles accordingly.
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);
};
<!doctype html>
<html lang="en">
<head>
<title>Arbitrary map extents</title>
<meta charset="UTF-8" />
<meta name="name" content="arbitrary_map_extents" />
<meta name="description" content="Create maps with arbitrary extents." />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="https://giro3d.org/images/favicon.svg" />
<link href="https://giro3d.org/assets/bootstrap-custom.css" rel="stylesheet" />
<script src="https://giro3d.org/assets/bootstrap.bundle.min.js"></script>
<link
rel="stylesheet"
type="text/css"
href="https://giro3d.org/next/css/example.css"
/>
</head>
<body>
<div id="viewerDiv" class="m-0 p-0 w-100 h-100"></div>
<div id="panelDiv" class="position-absolute top-0 start-0 mh-100 overflow-auto"></div>
<div class="side-pane-with-status-bar">
<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>
{
"name": "arbitrary_map_extents",
"dependencies": {
"@giro3d/giro3d": "git+https://gitlab.com/giro3d/giro3d.git"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}