Example showing vector tiles in EPSG:4326 (styled using ol-mapbox-style)
Example showing vector tiles in EPSG:4326 (styled using ol-mapbox-style
) loaded from maptiler.com. Note: Make sure to get your own API key at https://www.maptiler.com/cloud/ when using this example. No map will be visible when the API key has expired.
import VectorTileLayer from 'ol/layer/VectorTile.js';
import VectorTileSource from 'ol/source/VectorTile.js';
import {Map, View} from 'ol/index.js';
import {applyBackground, applyStyle} from 'ol-mapbox-style';
import {createXYZ} from 'ol/tilegrid.js';
const key = 'Get your own API key at https://www.maptiler.com/cloud/';
const url = 'https://api.maptiler.com/maps/basic-4326/style.json?key=' + key;
// Match the server resolutions
const tileGrid = createXYZ({
extent: [-180, -90, 180, 90],
tileSize: 512,
maxResolution: 180 / 512,
maxZoom: 13,
});
const layer = new VectorTileLayer({
declutter: true,
source: new VectorTileSource({
projection: 'EPSG:4326',
tileGrid: tileGrid,
}),
});
applyStyle(layer, url, '', {resolutions: tileGrid.getResolutions()});
applyBackground(layer, url);
const map = new Map({
target: 'map',
layers: [layer],
view: new View({
projection: 'EPSG:4326',
zoom: 0,
center: [0, 30],
}),
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vector tiles in EPSG:4326</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<div id="map" class="map" style="background:none;"></div>
<script type="module" src="main.js"></script>
</body>
</html>
{
"name": "vector-tiles-4326",
"dependencies": {
"ol": "10.2.1",
"ol-mapbox-style": "^12.3.1"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}