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

Search Control

Previous Next

A search box that finds places and moves the map to the one you pick.

<script lang="ts">
	import {
		Map,
		MapControlContainer,
		MapMarker,
		MapMarkerLabel,
		MapZoomControl
	} from '$lib/components/ui/map/index.js';
	import { MapSearchControl } from '$lib/components/ui/map-search/index.js';
	import type { Place } from '$lib/components/ui/place-autocomplete/index.js';

	let picked = $state.raw<Place | null>(null);
</script>

<Map class="size-full" viewport={{ center: [-3.7038, 40.4168], zoom: 11, bearing: 0, pitch: 0 }}>
	{#if picked}
		<MapMarker lngLat={picked.position} aria-label={picked.name}>
			<MapMarkerLabel>{picked.name}</MapMarkerLabel>
		</MapMarker>
	{/if}
	<MapControlContainer position="top-left">
		<MapSearchControl placeholder="Search Madrid" onselect={(place) => (picked = place)} />
	</MapControlContainer>
	<MapControlContainer position="top-right">
		<MapZoomControl />
	</MapControlContainer>
</Map>

Type a place name and pick a result with the mouse or the arrow keys and Enter. The map flies to a place that is a point, such as an address or a cafe. For a place that covers an area, such as a city or a park, the map fits the area in view.

The search control is the Place Autocomplete box wired to the map. Results come from the Photon geocoder, built on OpenStreetMap data, which needs no API key.

Installation

It adds the Place Autocomplete box and needs the map.

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

Usage

Place the control inside a Control Container. The top-left corner leaves room for the results list.

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

<Map class="h-96">
	<MapControlContainer position="top-left">
		<MapSearchControl />
	</MapControlContainer>
</Map>

Using the result

onselect gets the chosen Place, after the map starts moving. Each place has an id, a name, a description with street, city and country, a kind, a position and, for areas, bounds. Use it to drop a marker, as in the preview above:

<script lang="ts">
	import type { Place } from '$lib/components/ui/place-autocomplete/index.js';

	let picked = $state.raw<Place | null>(null);
</script>

<Map class="h-96">
	{#if picked}
		<MapMarker lngLat={picked.position} aria-label={picked.name} />
	{/if}
	<MapControlContainer position="top-left">
		<MapSearchControl onselect={(place) => (picked = place)} />
	</MapControlContainer>
</Map>

Camera

  • zoom is the zoom for point places, and the furthest the map zooms in when fitting an area.
  • padding is the space in pixels kept around an area.
  • nearCenter ranks results near the middle of the map first. It is on by default. Turn it off for a search with no local bias.

When the user prefers reduced motion, the map jumps to the place instead of flying.

Search options

Every prop of Place Autocomplete passes through, such as placeholder, limit, lang, debounce and onerror. Bind query to read or set the text in the box. Escape closes the results, and a second Escape clears the box.

To use your own Photon server, set baseUrl and add its host to your Content Security Policy.

API Reference

MapSearchControl

Props

NameTypeDescription
zoom numberZoom for places that are a point; areas are fitted to the view. Defaults to 15.
padding numberSpace in pixels kept around an area when fitting it. Defaults to 48.
nearCenter booleanPrefer results near the middle of the map. Defaults to true.
query bindablestringText in the search box. Defaults to ''.
placeholder stringHint shown in the empty search box. Defaults to 'Search places'.
label stringName of the search box for screen readers. Defaults to 'Search places'.
baseUrl stringPhoton base URL. Defaults to the public instance.
limit numberMost results to show.
lang stringLanguage of the results: `default`, `de`, `en` or `fr`.
debounce numberMilliseconds to wait after the last keystroke.
minLength numberFewest characters that start a search.
timeout numberMilliseconds before a request gives up.
class string

Events

NameTypeDescription
onselect (place: Place) => voidCalled with the place the user picks.
onerror (error: Error) => voidNetwork and response errors. Logged when unset.
Basemap Control Draw Control

On This Page

InstallationUsageUsing the resultCameraSearch optionsAPI Reference

Handcrafted by Oggy. The source code is on GitHub.