shadcn-svelte map
DocsComponentsBlocks
Get Started
  • Introduction
  • Installation
Guides
  • Theming
  • Drawing
  • Accessibility
  • Performance
  • Content Security Policy
Core
  • Map
  • Control Container
Markers
  • Marker
  • Popup
  • Point Layer
Data
  • GeoJSON
  • Shapes
  • Route
  • Arc
  • Cluster Layer
  • Heatmap
Controls
  • Zoom Control
  • Compass Control
  • Scale Control
  • Fullscreen Control
  • Locate Control
  • Basemap Control
  • Search Control
  • Draw Control
More
  • Place Autocomplete
  • Utilities

Installation

Previous Next

Add the map to a SvelteKit project with the shadcn-svelte CLI.

Requirements

  • SvelteKit 2 or 3 with Svelte 5.40 or newer.
  • Tailwind CSS v4.
  • shadcn-svelte, set up with init. If you have not done that yet:
pnpm dlx shadcn-svelte@latest init
npx shadcn-svelte@latest init
yarn dlx shadcn-svelte@latest init
bunx --bun shadcn-svelte@latest init

Add the map

pnpm dlx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map.json
npx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map.json
yarn dlx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map.json
bunx --bun shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map.json

This copies the map components into src/lib/components/ui/map, adds the shadcn items they use (button, button group, dropdown menu, spinner) and installs these packages:

  • maplibre-gl renders the map.
  • @lucide/svelte provides the icons.

The MapLibre worker is bundled with your app through Vite, so the map loads nothing from a CDN.

Your first map

Give the map a height. It fills its parent’s width.

<script lang="ts">
	import { Map, MapMarker } from '$lib/components/ui/map/index.js';
</script>

<Map
	class="h-96 rounded-lg border"
	viewport={{ center: [13.405, 52.52], zoom: 11, bearing: 0, pitch: 0 }}
>
	<MapMarker lngLat={[13.405, 52.52]} aria-label="Berlin" />
</Map>

The map renders in the browser only. On the server it renders an empty frame, so it works with server-side rendering and prerendering without extra checks.

Optional: skip the first reload in dev

The map loads maplibre-gl only in the browser, so Vite finds it the first time a page with a map opens. It then bundles it and reloads the page once, and the console shows a 504 (Outdated Optimize Dep) error for that first load. The map works after the reload.

To avoid this, tell Vite about maplibre-gl up front in vite.config.ts:

export default defineConfig({
	optimizeDeps: { include: ['maplibre-gl'] },
	plugins: [tailwindcss(), sveltekit()]
});

This only changes the dev server. Production builds work the same either way.

Add a block

Blocks are complete examples, such as a store locator. Each one installs into its own folder and pulls in the map if you do not have it yet.

pnpm dlx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/store-locator.json
npx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/store-locator.json
yarn dlx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/store-locator.json
bunx --bun shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/store-locator.json

Browse them all in the block gallery.

Drawing and place search

The drawing toolbar and the map search box are separate items, so a plain map does not install a drawing engine or a geocoder. Add them when you need them:

pnpm dlx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map-draw.json
npx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map-draw.json
yarn dlx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map-draw.json
bunx --bun shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map-draw.json
pnpm dlx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map-search.json
npx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map-search.json
yarn dlx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map-search.json
bunx --bun shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map-search.json

map-draw goes into src/lib/components/ui/map-draw and installs terra-draw and its MapLibre adapter. map-search goes into src/lib/components/ui/map-search and adds the place search box. Blocks that use them pull them in for you.

Place search on its own

The search box behind the map search control is its own item. Add it if you want place search without a map:

pnpm dlx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/place-autocomplete.json
npx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/place-autocomplete.json
yarn dlx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/place-autocomplete.json
bunx --bun shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/place-autocomplete.json

Updating

Run the same add command again. The CLI asks before it overwrites files you changed.

Introduction Theming

On This Page

RequirementsAdd the mapYour first mapOptional: skip the first reload in devAdd a blockDrawing and place searchPlace search on its ownUpdating

Handcrafted by Oggy. The source code is on GitHub.