JS MapLibre Adapter: autodetect minzoom/maxzoom, clean up hacky loading syntax [#48]

This commit is contained in:
Brandon Liu
2022-10-14 00:08:08 +08:00
parent b1c7cf59a3
commit 9f6941e969

View File

@@ -62,6 +62,27 @@ export class Protocol {
} }
tile = (params: any, callback: any) => { tile = (params: any, callback: any) => {
if (params.type == "json") {
const pmtiles_url = params.url.substr(10);
let instance = this.tiles.get(pmtiles_url);
if (!instance) {
instance = new PMTiles(pmtiles_url);
this.tiles.set(pmtiles_url, instance);
}
instance.getHeader().then((h) => {
const tilejson = {
tiles: [params.url + "/{z}/{x}/{y}"],
minzoom: h.minZoom,
maxzoom: h.maxZoom,
};
callback(null, tilejson, null, null);
});
return {
cancel: () => {},
};
} else {
const re = new RegExp(/pmtiles:\/\/(.+)\/(\d+)\/(\d+)\/(\d+)/); const re = new RegExp(/pmtiles:\/\/(.+)\/(\d+)\/(\d+)\/(\d+)/);
const result = params.url.match(re); const result = params.url.match(re);
const pmtiles_url = result[1]; const pmtiles_url = result[1];
@@ -81,19 +102,28 @@ export class Protocol {
controller.abort(); controller.abort();
}; };
instance.getZxy(+z, +x, +y, signal).then((resp) => { instance
.getZxy(+z, +x, +y, signal)
.then((resp) => {
if (resp) { if (resp) {
callback(null, new Uint8Array(resp.data), resp.cacheControl, resp.expires); callback(
null,
new Uint8Array(resp.data),
resp.cacheControl,
resp.expires
);
} else { } else {
callback(null, new Uint8Array(), null, null); callback(null, new Uint8Array(), null, null);
} }
}).catch((e) => { })
.catch((e) => {
if (e.name !== "AbortError") { if (e.name !== "AbortError") {
throw e; throw e;
} }
}); });
return { return {
cancel: cancel cancel: cancel,
}; };
}
}; };
} }