Skip to content

Instantly share code, notes, and snippets.

@krishnaglodha
Created March 18, 2025 07:15
Show Gist options
  • Save krishnaglodha/0fc604c3e318f006b9815ca7746ba61b to your computer and use it in GitHub Desktop.
Save krishnaglodha/0fc604c3e318f006b9815ca7746ba61b to your computer and use it in GitHub Desktop.
Load GeoServer Vector Tile in Openlayers map with EPSG:4326 projection
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>GeoServer WMTS with Mapbox Vector Tile in OpenLayers</title>
<link rel="stylesheet" href="https://unpkg.com/ol/ol.css" />
<script src="https://unpkg.com/ol/dist/ol.js"></script>
</head>
<body>
<div id="map" style="width: 100%; height: 500px"></div>
<script>
var projection = ol.proj.get("EPSG:4326");
var projectionExtent = projection.getExtent();
var matrixIds = new Array(22);
for (var z = 0; z < 22; ++z) {
matrixIds[z] = "EPSG:4326:" + z;
}
resolutions = [
0.703125, 0.3515625, 0.17578125, 0.087890625, 0.0439453125,
0.02197265625, 0.010986328125, 0.0054931640625, 0.00274658203125,
0.001373291015625, 6.866455078125e-4, 3.4332275390625e-4,
1.71661376953125e-4, 8.58306884765625e-5, 4.291534423828125e-5,
2.1457672119140625e-5, 1.0728836059570312e-5, 5.364418029785156e-6,
2.682209014892578e-6, 1.341104507446289e-6, 6.705522537231445e-7,
3.3527612686157227e-7,
];
bouldersLayerWMTS = new ol.layer.VectorTile({
source: new ol.source.VectorTile({
url: "https://YOUR-GEOSERVER/geoserver/gwc/service/wmts?REQUEST=GetTile&SERVICE=WMTS&VERSION=1.0.0&LAYER=WORKSPACE:LAYER&STYLE=&TILEMATRIX=EPSG:4326:{z}&TILEMATRIXSET=EPSG:4326&FORMAT=application/vnd.mapbox-vector-tile&TILECOL={x}&TILEROW={y}",
wrapX: true,
format: new ol.format.MVT(),
projection: projection,
tileGrid: new ol.tilegrid.WMTS({
tileSize: [256, 256],
origin: ol.extent.getTopLeft(projectionExtent),
resolutions: resolutions,
matrixIds: matrixIds,
}),
}),
});
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM(),
}),
bouldersLayerWMTS,
],
target: "map",
view: new ol.View({
projection: projection,
center: [-74.1446553, 40.5],
zoom: 10,
}),
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment