mirror of
https://github.com/protomaps/PMTiles.git
synced 2026-02-04 02:41:09 +00:00
74 lines
2.5 KiB
HTML
74 lines
2.5 KiB
HTML
<html>
|
|
<head>
|
|
<title>PMTiles OpenLayers Raster Example</title>
|
|
<meta charset="utf-8"/>
|
|
<script src="https://cdn.jsdelivr.net/npm/ol@v7.3.0/dist/ol.js"></script>
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@v7.3.0/ol.css">
|
|
<script src="https://unpkg.com/pmtiles@2.7.2/dist/index.js"></script>
|
|
<style>
|
|
body, #map {
|
|
height:100vh;
|
|
margin: 0;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="map"></div>
|
|
<script type="text/javascript">
|
|
class RasterPMTilesSource extends ol.source.DataTile {
|
|
loadImage = (src) => {
|
|
return new Promise((resolve, reject) => {
|
|
const img = new Image();
|
|
img.addEventListener('load', () => resolve(img));
|
|
img.addEventListener('error', () => reject(new Error('load failed')));
|
|
img.src = src;
|
|
});
|
|
}
|
|
|
|
constructor(options) {
|
|
super({
|
|
state: "loading",
|
|
attributions: options.attributions
|
|
});
|
|
|
|
const p = new pmtiles.PMTiles(options.url);
|
|
p.getHeader().then(h => {
|
|
// TODO: set extent based on header
|
|
this.tileGrid.minZoom = h.minZoom;
|
|
this.tileGrid.maxZoom = h.maxZoom;
|
|
this.setLoader(async (z,x,y) => {
|
|
const response = await p.getZxy(z, x, y);
|
|
const blob = new Blob([response.data]);
|
|
const src = URL.createObjectURL(blob);
|
|
const image = await this.loadImage(src);
|
|
image.width = 512;
|
|
URL.revokeObjectURL(src);
|
|
return image;
|
|
});
|
|
this.setState("ready");
|
|
})
|
|
}
|
|
}
|
|
|
|
// TODO: tile size 512,512
|
|
const rasterLayer = new ol.layer.WebGLTile({
|
|
source: new RasterPMTilesSource({
|
|
url:"https://r2-public.protomaps.com/protomaps-sample-datasets/terrarium_z9.pmtiles",
|
|
attributions:["https://github.com/tilezen/joerd/blob/master/docs/attribution.md"]
|
|
})
|
|
});
|
|
|
|
ol.proj.useGeographic();
|
|
const map = new ol.Map({
|
|
layers: [rasterLayer],
|
|
target: 'map',
|
|
view: new ol.View({
|
|
center: [0,0],
|
|
zoom: 0,
|
|
multiWorld: true
|
|
}),
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|