// @ts-ignore const html = require("yo-yo"); const mapboxgl = require("mapbox-gl"); const startMapService = require("./"); startMapService("./sw.js"); const root = html`
`; document.body.appendChild(root); const values = { accessToken: "pk.eyJ1IjoiZGlnaWRlbSIsImEiOiJuM3FabmNFIn0._gF6262MSzePWUChu4S9PA", longitude: 0, latitude: 0, zoom: 0, style: "mapbox://styles/mapbox/outdoors-v11", width: 300, height: 300, pixelRatio: window.devicePixelRatio, }; /** @param {{id: keyof typeof values, type?: string}} options */ const input = ({ id, type = "text" }) => { /** @param {{target: HTMLInputElement}} event */ function handleInput({ target }) { if (!target || target.value === "") return; // @ts-ignore values[id] = type === "number" ? target.valueAsNumber : target.value; updateMap(); } return html`
`; }; function updateMap() { console.log("update map", values); map.jumpTo({ center: [values.longitude, values.latitude], zoom: values.zoom, }); try { new URL(values.style); map.setStyle(values.style); } catch (e) {} } /** * @param {keyof typeof values} id * @returns {number | string} */ function getValue(id) { return values[id]; } function generateImageUrl() { const { longitude, latitude, zoom, accessToken, width, height, style, pixelRatio, } = values; return `/static-maps/${longitude},${latitude},${zoom}/${width}x${height}@${pixelRatio}x?accessToken=${accessToken}&style=${style}`; } const form = () => html`

Parameters

${input({ id: "accessToken" })} ${input({ id: "style" })}
${input({ id: "longitude", type: "number" })} ${input({ id: "latitude", type: "number" })}
${input({ id: "zoom", type: "number" })}
${input({ id: "width", type: "number" })} ${input({ id: "height", type: "number" })}
${input({ id: "pixelRatio", type: "number" })}

`; /** @param {string} src */ const mapImage = (src) => html``; const mapEl = html`
`; const formEl = form(); const imageEl = mapImage(generateImageUrl()); html.update( root, html`
${formEl}
${mapEl}
${imageEl}
` ); const map = new mapboxgl.Map({ accessToken: values.accessToken, center: [values.longitude, values.latitude], zoom: values.zoom, style: values.style, container: mapEl, }); map.on("moveend", (e) => { const center = map.getCenter(); const zoom = map.getZoom(); values.longitude = Math.round(center.lng * 10000) / 10000; values.latitude = Math.round(center.lat * 10000) / 10000; values.zoom = Math.round(zoom * 100) / 100; html.update(formEl, form()); });