diff --git a/js/adapters.ts b/js/adapters.ts index 51a8035a..52d26aaa 100644 --- a/js/adapters.ts +++ b/js/adapters.ts @@ -123,20 +123,38 @@ export class Protocol { this.tiles.set(pmtiles_url, instance); } - instance - .getHeader() - .then((h) => { + const loadTileJSON = async (i: PMTiles) => { + try { + const header = await i.getHeader(); + const hash = new URLSearchParams( + new URL(pmtiles_url).hash.substring(1) + ); + let metadata: any = {}; + if (hash.get("metadata")) { + metadata = await i.getMetadata(); + } + const tilejson = { tiles: [params.url + "/{z}/{x}/{y}"], - minzoom: h.minZoom, - maxzoom: h.maxZoom, - bounds: [h.minLon, h.minLat, h.maxLon, h.maxLat], + minzoom: header.minZoom, + maxzoom: header.maxZoom, + bounds: [ + header.minLon, + header.minLat, + header.maxLon, + header.maxLat, + ], + attribution: metadata.attribution, + vector_layers: metadata.vector_layers, }; + callback(null, tilejson, null, null); - }) - .catch((e) => { + } catch (e: any) { callback(e, null, null, null); - }); + } + }; + + loadTileJSON(instance); return { cancel: () => {}, diff --git a/js/examples/maplibre.html b/js/examples/maplibre.html index 41136ccb..23abf8a3 100644 --- a/js/examples/maplibre.html +++ b/js/examples/maplibre.html @@ -39,8 +39,7 @@ sources: { "example_source": { type: "vector", - url: "pmtiles://" + PMTILES_URL, - attribution: '© OpenStreetMap' + url: "pmtiles://" + PMTILES_URL + "#metadata=true" } }, layers: [