All blocks
Store locator
EditingSearch an address to list the nearest stores, and pin spots for new ones.
Find a store
Search an address or drag your pin. Use the pin tool to propose a new site.
- 996 mStationsplein 7 · 07:00–22:00
- 1.3 kmWesterstraat 12 · 08:00–20:00
- 1.7 kmFerdinand Bolstraat 40 · 08:00–21:00
- 2.0 kmKinkerstraat 88 · 08:00–20:00
- 3.2 kmJavaplein 3 · 09:00–19:00
- 3.2 kmBeethovenstraat 70 · 08:00–20:00
- 4.5 kmBuikslotermeerplein 1 · 09:00–18:00
Use the arrow keys to pan, plus and minus to zoom, and Shift with the arrow keys to rotate.
Install
Adds the block to src/lib/components/store-locator and the map if you do not have
it yet.
Code
<script lang="ts">
import NavigationIcon from '@lucide/svelte/icons/navigation';
import StoreIcon from '@lucide/svelte/icons/store';
import { cn } from '$lib/utils.js';
import { Badge } from '$lib/components/ui/badge/index.js';
import * as Card from '$lib/components/ui/card/index.js';
import { Input } from '$lib/components/ui/input/index.js';
import {
Map,
MapControlContainer,
MapMarker,
MapMarkerContent,
MapMarkerPopup,
MapMarkerTooltip,
MapZoomControl,
distanceMeters,
type LngLatTuple,
type MapStyle,
type MapViewport
} from '$lib/components/ui/map/index.js';
import {
MapDrawControl,
type DrawFeature,
type DrawTool
} from '$lib/components/ui/map-draw/index.js';
import { MapSearchControl } from '$lib/components/ui/map-search/index.js';
import { START, STORES, VIEWPORT } from './data.js';
interface Props {
/** Basemap. Defaults to OpenFreeMap. */
mapStyle?: MapStyle;
class?: string;
}
let { mapStyle, class: className }: Props = $props();
interface Listing {
key: string;
name: string;
detail: string;
lngLat: LngLatTuple;
distance: number;
proposed: boolean;
}
const PICK_ZOOM = 15;
const METRES_PER_KM = 1_000;
let viewport = $state<MapViewport>(VIEWPORT);
let origin = $state<LngLatTuple>(START);
let selected = $state<string | null>(null);
let tool = $state<DrawTool>('none');
let proposals = $state.raw<DrawFeature[]>([]);
// Kept by drawing id and never pruned, so an undone delete gets its name back.
let proposalNames = $state.raw<Record<string, string>>({});
const listings: Listing[] = $derived.by(() => {
const stores = STORES.map((store): Listing => ({
key: store.id,
name: store.name,
detail: `${store.address} · ${store.hours}`,
lngLat: store.lngLat,
distance: distanceMeters(origin, store.lngLat),
proposed: false
}));
const proposed = proposals.flatMap((feature, index): Listing[] => {
if (feature.geometry.type !== 'Point') return [];
const key = String(feature.id);
const lngLat = feature.geometry.coordinates as LngLatTuple;
return [
{
key,
name: proposalNames[key] ?? `Proposed site ${index + 1}`,
detail: 'Not open yet',
lngLat,
distance: distanceMeters(origin, lngLat),
proposed: true
}
];
});
return [...stores, ...proposed].sort((a, b) => a.distance - b.distance);
});
function formatDistance(metres: number): string {
if (metres < METRES_PER_KM) return `${Math.round(metres)} m`;
return `${(metres / METRES_PER_KM).toFixed(1)} km`;
}
function show(listing: Listing) {
selected = listing.key;
viewport = { ...viewport, center: listing.lngLat, zoom: Math.max(viewport.zoom, PICK_ZOOM) };
}
function rename(key: string, value: string) {
proposalNames = { ...proposalNames, [key]: value };
}
</script>
<div class={cn('grid min-h-[560px] gap-4 p-4 lg:grid-cols-[20rem_minmax(0,1fr)]', className)}>
<Card.Root class="min-h-0 gap-4 overflow-y-auto">
<Card.Header>
<Card.Title>Find a store</Card.Title>
<Card.Description>
Search an address or drag your pin. Use the pin tool to propose a new site.
</Card.Description>
</Card.Header>
<Card.Content>
<ol class="flex flex-col gap-2" aria-label="Stores by distance">
{#each listings as listing (listing.key)}
<li
class={[
'flex flex-col gap-2 rounded-md border p-3',
selected === listing.key && 'border-primary'
]}
data-testid="listing"
data-key={listing.key}
>
<div class="flex items-start justify-between gap-2">
{#if listing.proposed}
<Input
value={listing.name}
aria-label="Proposed site name"
class="h-8"
oninput={(event) => rename(listing.key, event.currentTarget.value)}
/>
{:else}
<button
type="button"
class="text-left font-medium hover:underline"
onclick={() => show(listing)}
>
{listing.name}
</button>
{/if}
<span class="shrink-0 text-sm text-muted-foreground tabular-nums">
{formatDistance(listing.distance)}
</span>
</div>
<div class="flex items-center justify-between gap-2 text-sm text-muted-foreground">
<span class="truncate">{listing.detail}</span>
{#if listing.proposed}
<Badge variant="outline">Proposed</Badge>
{/if}
</div>
</li>
{/each}
</ol>
</Card.Content>
</Card.Root>
<Map class="min-h-[360px] rounded-lg border" aria-label="Store map" {mapStyle} bind:viewport>
{#each STORES as store (store.id)}
<MapMarker lngLat={store.lngLat} aria-label="{store.name} store">
<MapMarkerContent>
<span
class={[
'grid size-7 place-items-center rounded-full border-2 border-background shadow-md',
selected === store.id
? 'bg-primary text-primary-foreground'
: 'bg-background text-foreground'
]}
>
<StoreIcon class="size-3.5" />
</span>
</MapMarkerContent>
<MapMarkerTooltip>{store.name}</MapMarkerTooltip>
<MapMarkerPopup aria-label="{store.name} store">
<p class="font-medium">{store.name}</p>
<p class="text-muted-foreground">{store.address}</p>
<p class="text-muted-foreground">Open {store.hours}</p>
</MapMarkerPopup>
</MapMarker>
{/each}
<MapMarker bind:lngLat={origin} aria-label="Your location, drag to move" draggable>
<MapMarkerContent>
<span
class="grid size-8 place-items-center rounded-full bg-primary text-primary-foreground shadow-md ring-4 ring-primary/25"
>
<NavigationIcon class="size-4" />
</span>
</MapMarkerContent>
<MapMarkerTooltip>You are here</MapMarkerTooltip>
</MapMarker>
<MapControlContainer position="top-left">
<MapSearchControl
class="w-64"
placeholder="Search an address"
onselect={(place) => (origin = place.position)}
/>
<MapDrawControl
orientation="horizontal"
modes={['point']}
bind:mode={tool}
onchange={(features) => (proposals = features)}
/>
</MapControlContainer>
<MapControlContainer position="top-right">
<MapZoomControl />
</MapControlContainer>
</Map>
</div>
import type { LngLatTuple, MapViewport } from '$lib/components/ui/map/index.js';
export interface Store {
id: string;
name: string;
address: string;
hours: string;
lngLat: LngLatTuple;
}
/** Central Amsterdam. */
export const VIEWPORT: MapViewport = {
center: [4.895, 52.37],
zoom: 12.5,
bearing: 0,
pitch: 0
};
/** Where the shopper starts before searching or dragging the pin. */
export const START: LngLatTuple = [4.8952, 52.3702];
export const STORES: Store[] = [
{
id: 'jordaan',
name: 'Jordaan',
address: 'Westerstraat 12',
hours: '08:00–20:00',
lngLat: [4.8812, 52.3781]
},
{
id: 'de-pijp',
name: 'De Pijp',
address: 'Ferdinand Bolstraat 40',
hours: '08:00–21:00',
lngLat: [4.8925, 52.3553]
},
{
id: 'oost',
name: 'Oost',
address: 'Javaplein 3',
hours: '09:00–19:00',
lngLat: [4.9405, 52.3638]
},
{
id: 'west',
name: 'Oud-West',
address: 'Kinkerstraat 88',
hours: '08:00–20:00',
lngLat: [4.8668, 52.3662]
},
{
id: 'noord',
name: 'Noord',
address: 'Buikslotermeerplein 1',
hours: '09:00–18:00',
lngLat: [4.9411, 52.3999]
},
{
id: 'centraal',
name: 'Centraal',
address: 'Stationsplein 7',
hours: '07:00–22:00',
lngLat: [4.9003, 52.3786]
},
{
id: 'zuid',
name: 'Zuid',
address: 'Beethovenstraat 70',
hours: '08:00–20:00',
lngLat: [4.8774, 52.3434]
}
];