Plain Map
Bounds: -32.0000, -8.0000, 63.0000, 41.0000
Idle
<script lang="ts">
import * as maplibregl from 'maplibre-gl';
import maplibreWorkerUrl from 'maplibre-gl/dist/maplibre-gl-worker.mjs?worker&url';
import MapLibre from 'svelte-maplibre/MapLibre.svelte';
import CodeSample from '$site/CodeSample.svelte';
import type { LngLatBoundsLike } from 'maplibre-gl';
import code from './+page.svelte?raw';
import type { MapMoveEvent } from 'svelte-maplibre/types';
maplibregl.setWorkerUrl(maplibreWorkerUrl);
let bounds: LngLatBoundsLike = $state([-32, -8, 63, 41]);
let displayBounds = $derived(bounds.map((b) => b.toFixed(4)).join(', '));
let dragging = $state(false);
let zooming = $state(false);
// Updated on every frame of a pan or zoom, not only when the movement ends.
let liveView = $state('');
function onmove(e: MapMoveEvent) {
const center = e.target.getCenter();
const zoom = e.target.getZoom();
liveView = `Center: ${center.lng.toFixed(4)}, ${center.lat.toFixed(4)} ยท Zoom: ${zoom.toFixed(2)}`;
}
let status = $derived(
[dragging && 'Dragging the map', zooming && 'Zooming'].filter(Boolean).join(', ') || 'Idle'
);
</script>
<p class="tabular-nums">Bounds: {displayBounds}</p>
<p class="tabular-nums" data-testid="live-view">{liveView}</p>
<p data-testid="map-status">{status}</p>
<MapLibre
style="https://basemaps.cartocdn.com/gl/positron-gl-style/style.json"
class="relative aspect-9/16 max-h-[70vh] w-full sm:aspect-video sm:max-h-full"
standardControls
bind:bounds
ondragstart={() => (dragging = true)}
ondragend={() => (dragging = false)}
onzoomstart={() => (zooming = true)}
onzoomend={() => (zooming = false)}
{onmove}
/>Components used: MapLibre