<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.jsonnpx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map-search.jsonyarn dlx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map-search.jsonbunx --bun shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/map-search.jsonUsage
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
zoomis the zoom for point places, and the furthest the map zooms in when fitting an area.paddingis the space in pixels kept around an area.nearCenterranks 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
| Name | Type | Description |
|---|---|---|
zoom | number | Zoom for places that are a point; areas are fitted to the view. Defaults to 15. |
padding | number | Space in pixels kept around an area when fitting it. Defaults to 48. |
nearCenter | boolean | Prefer results near the middle of the map. Defaults to true. |
query bindable | string | Text in the search box. Defaults to ''. |
placeholder | string | Hint shown in the empty search box. Defaults to 'Search places'. |
label | string | Name of the search box for screen readers. Defaults to 'Search places'. |
baseUrl | string | Photon base URL. Defaults to the public instance. |
limit | number | Most results to show. |
lang | string | Language of the results: `default`, `de`, `en` or `fr`. |
debounce | number | Milliseconds to wait after the last keystroke. |
minLength | number | Fewest characters that start a search. |
timeout | number | Milliseconds before a request gives up. |
class | string |
Events
| Name | Type | Description |
|---|---|---|
onselect | (place: Place) => void | Called with the place the user picks. |
onerror | (error: Error) => void | Network and response errors. Logged when unset. |