mapoffline.js Module Documentation

Bahasa Indonesia

mapoffline.js is an add-on module for map.js that displays a base map from a single PMTiles file — a single-file format for map tiles. That file can be:

PMTiles version 3 containing raster tiles (PNG, JPEG, WebP, AVIF) or, since 0.0.30, vector tiles (MVT). Vector tiles are drawn in the browser by mapvector.js: the file is much smaller for the same area, and the map stays sharp above the file’s highest zoom.

Usage (Import)

import { createMap } from "https://cdn.jsdelivr.net/gh/crootjs/lib@0.0.43/map.js";
import { openPMTiles } from "https://cdn.jsdelivr.net/gh/crootjs/lib@0.0.43/mapoffline.js";

openPMTiles(source, responseFunction, options)

Opens a PMTiles file. source: a URL, or a File/Blob.

responseFunction(archive, error) is called once; the function also returns a Promise of archive. On failure (not PMTiles, a different version, unsupported compression, server refused), archive = null and error holds the reason.

options (optional, only affects vector tiles):

Option Default Description
language — Language code for place names, e.g. "id": uses name:id if present in the data, otherwise name
style default style Your own style rules (see mapvector.js)
tileSize 512 Image resolution of each tile in pixels; 512 is sharp on retina screens
overzoom 4 How many levels above the file’s highest zoom are still drawn

archive can be used directly as the tiles option in createMap or setTiles:

Key Contents
getTile(z, x, y) Promise of the tile image Blob, or null if the file has no tile there
minZoom, maxZoom Zoom range. For vector tiles maxZoom already includes overzoom
fileMaxZoom Highest zoom present in the file
bounds [minLng, minLat, maxLng, maxLat] the file’s coverage
center, centerZoom Center point and zoom suggested by the file
tileType "image/png", "image/jpeg", "image/webp", "image/avif", or "mvt" (vector)
metadata, attribution JSON metadata inside the file, and its attribution as text (HTML tags stripped) if any

Since 0.0.30 map.js applies archive.attribution itself when the attribution option is not set.

const arsip = await openPMTiles("https://contoh.id/peta/bandung.pmtiles");
createMap("peta", {
    tiles: arsip,
    maxNativeZoom: arsip.maxZoom, // above this, the last tiles are scaled up
    maxZoom: arsip.maxZoom + 2,
    center: arsip.center,
    zoom: arsip.centerZoom,
    attribution: arsip.attribution || "© OpenStreetMap contributors",
});

Vector tiles

const arsip = await openPMTiles("https://contoh.id/peta/bandung-vektor.pmtiles", null, { language: "id" });
createMap("peta", {
    tiles: arsip,
    maxZoom: arsip.maxZoom, // a file up to z15 stays sharp up to z19
    center: arsip.center,
    zoom: arsip.centerZoom,
});

The default style targets the Protomaps Basemap schema, used by files from pmtiles extract of the Protomaps world map. Files with other schemas still display, with a generic style (gray areas, lines, point labels). For your own colors, see mapvector.js.

Without internet

<input type="file" id="berkas" accept=".pmtiles">
<div id="peta" class="peta"></div>
import { onChange } from "https://cdn.jsdelivr.net/gh/crootjs/lib@0.0.43/element.js";
import { setTiles } from "https://cdn.jsdelivr.net/gh/crootjs/lib@0.0.43/map.js";

createMap("peta", { tiles: false, center: [107.6, -6.9], zoom: 12 });
onChange("berkas", async () => {
    const arsip = await openPMTiles(document.getElementById("berkas").files[0]);
    if (arsip) setTiles("peta", arsip, { maxNativeZoom: arsip.maxZoom });
});

A file from <input type="file"> is read directly from the device, so once the page is open the map keeps working even if the signal drops. To make the page itself open without internet too, install a service worker or save the page as a PWA.

Creating PMTiles files

Notes