Skip to content

Integrate Map View Links

Add a view reader and writer around your viewer’s existing map or camera state. The TypeScript API reference covers the core and browser APIs.

Use the map link converter to try a full provider URL or coordinates.

Install the package

Install @alpenverse/map-view-links from the versioned archive hosted on these docs.

Terminal window
pnpm add https://alpenverse.github.io/map-view-links/downloads/alpenverse-map-view-links-0.1.2.tgz

Download v0.1.2 · SHA-256 checksum

Read and write a view link

  • map:47.047,8.474,7g opens a north-up map. At the center, one CSS pixel toward the top covers seven ground metres. Use g for fresh map shares.
  • cam:47.047,8.474,1800,90h,45t places the eye 1,800 metres above mean sea level, facing east and tilted 45° up from straight down.

The core package turns a view into a URL and reads it back without loading a viewer.

import { createViewUrl, readView } from '@alpenverse/map-view-links';
const url = createViewUrl('https://viewer.example/map?layer=topo', {
kind: 'map',
latitude: 47.047,
longitude: 8.474,
groundResolution: 7,
});
if (url) {
const result = readView({ search: url.search });
if (result.status === 'valid') console.log(result.view);
}

Connect a browser viewer

Call connectViewLink once your viewer is ready. Supply three callbacks from your SDK integration.

  • apply(view, link) restores a valid incoming view. For an asynchronous restore, resolve the promise once capture() can return the restored view.
  • capture() reads the live map or camera state in shared view units. Return undefined while there is no view to share.
  • subscribe(update) calls update when a capture becomes available or the view changes and returns a function that removes the listener.
import { connectViewLink, type ViewLinkAdapter } from '@alpenverse/map-view-links/browser';
export async function addViewLinks(
{ apply, capture, subscribe }: ViewLinkAdapter,
copyButton: HTMLButtonElement,
showNotice: (message: string) => void,
) {
const link = await connectViewLink({ apply, capture, subscribe }, {
onRead(result) {
if (result.status === 'invalid') showNotice(result.message);
},
});
async function copy() {
try {
await navigator.clipboard.writeText(link.getCurrentViewUrl().href);
} catch {
showNotice('Could not copy the view link.');
}
}
copyButton.addEventListener('click', copy);
return () => {
copyButton.removeEventListener('click', copy);
link.dispose();
};
}

The default reads and writes the query and leaves the fragment intact. If your app owns view parameters in both carriers, pass { ownsFragment: true } to write to the query or { carrier: "fragment" } to write to the fragment. The helper accepts one incoming view and removes a stale view from the other carrier when it writes.

Call the returned cleanup function when the viewer is removed. If your router handles the initial restore, attach watchViewLink after the view settles. To attach while capture is still unavailable, pass restoredCapture with the view the adapter actually applied, including any snapping or clamping.

Your app handles Back and Forward with either helper. Dispose the old watcher to cancel pending writes, restore the new entry, then attach a new watcher.

Browse the full SDK demo gallery or jump to 2D-only maps, 2D and 3D viewers, or graphics engines. Each demo includes a code panel showing how it reads, applies, and saves views.

Copy a view without updating the address bar

getCurrentViewUrl() can update the address bar. { onUnsupportedScale: false } suppresses the scale notice for that call. For a clipboard-only share or preview, use getCurrentView() with createViewUrl(). This example writes the share to the query and clears any view parameter from the app-owned fragment. Omit ownsFragment if the fragment belongs to your router or anchors.

import { createViewUrl } from '@alpenverse/map-view-links';
import type { ViewLinkSession } from '@alpenverse/map-view-links/browser';
export async function copyCurrentView(
session: ViewLinkSession,
showNotice: (message: string) => void,
) {
const capture = session.getCurrentView();
const url = capture && createViewUrl(window.location.href, capture, {
ownsFragment: true,
mapScaleFormat: 'g',
});
if (!url) {
showNotice('There is no view to share right now.');
return;
}
try {
await navigator.clipboard.writeText(url.href);
} catch {
showNotice('Could not copy the view link.');
}
}

A watcher can still sync scheduled navigation changes. An app that controls the entire URL can use capture() and createViewUrl() directly without attaching a watcher.

Use the format directly

Use URLSearchParams to carry a payload alongside existing application parameters. The receiver needs a reader that implements the format.

const url = new URL('https://viewer.example/map?layer=topo');
url.searchParams.set('view', 'map:47.047,8.474,7g');
console.log(url.href);
// https://viewer.example/map?layer=topo&view=map%3A47.047%2C8.474%2C7g

Implement a viewer adapter

  1. Read the query or host-controlled fragment according to the URL carrier rules. Validate the payload before applying it.
  2. Restore the position first. For a flat map, apply the heading and match the screen-up ground resolution at the center of the usable viewport after padding. For a camera, use the camera conventions.
  3. Report the view that was applied and any adapter limitations. Verify the result with the shared test cases.

AI integration brief

Copy this prompt into your coding assistant.