Map Components

Map components provide the foundation for all mapping functionality. They handle the core map setup, view management, and provide context for other components.

Map.Root

The main map container that creates the OpenLayers map instance and provides context to all child components.

Basic Usage

<script>
	import { Map } from 'svelte-openlayers';
</script>

<Map.Root class="h-96 w-full">
	<Map.View center={[0, 0]} zoom={2} />
</Map.Root>

Props

PropTypeDefaultDescription
classstringundefinedCSS classes for the map container
stylestringundefinedInline styles for the map container
targetHTMLElementundefinedTarget element for the map
pixelRationumberundefinedDevice pixel ratio
keyboardEventTargetHTMLElement | DocumentundefinedKeyboard event target
maxTilesLoadingnumberundefinedMaximum tiles loading simultaneously
moveTolerancenumberundefinedMove tolerance in pixels
zoomControlbooleantrueShow zoom control
attributionControlbooleantrueShow attribution control
rotateControlbooleanfalseShow rotate control
mousePositionControlbooleanfalseShow mouse position control
mapMap | nullnullBindable OpenLayers Map instance
viewView | nullnullBindable OpenLayers View instance

Events

Map.Root supports comprehensive event handling for map interactions. All events receive the appropriate OpenLayers event object.

MapBrowserEvent Handlers

Handle pointer and mouse interactions with the map:

<Map.Root
	onSingleclick={handleSingleClick}
	onPointermove={handlePointerMove}
	onPointerdown={handlePointerDown}
>
	<Map.View />
</Map.Root>
Event HandlerTypeDescription
onSingleclick(evt: MapBrowserEvent) => voidSingle click event
onClick(evt: MapBrowserEvent) => voidClick event
onDblclick(evt: MapBrowserEvent) => voidDouble click event
onPointerdrag(evt: MapBrowserEvent) => voidPointer drag event
onPointermove(evt: MapBrowserEvent) => voidPointer move event
onPointerdown(evt: MapBrowserEvent) => voidPointer down event
onPointerup(evt: MapBrowserEvent) => voidPointer up event
onPointerover(evt: MapBrowserEvent) => voidPointer over event
onPointerout(evt: MapBrowserEvent) => voidPointer out event
onPointerenter(evt: MapBrowserEvent) => voidPointer enter event
onPointerleave(evt: MapBrowserEvent) => voidPointer leave event
onPointercancel(evt: MapBrowserEvent) => voidPointer cancel event

MapEvent Handlers

Handle map lifecycle and navigation events:

<Map.Root
	onMovestart={handleMoveStart}
	onMoveend={handleMoveEnd}
	onLoadend={handleLoadEnd}
>
	<Map.View />
</Map.Root>
Event HandlerTypeDescription
onPostrender(evt: MapEvent) => voidAfter map rendering
onMovestart(evt: MapEvent) => voidMap movement starts
onMoveend(evt: MapEvent) => voidMap movement ends
onLoadstart(evt: MapEvent) => voidMap loading starts
onLoadend(evt: MapEvent) => voidMap loading ends

RenderEvent Handlers

Handle rendering lifecycle events:

<Map.Root
	onPrecompose={handlePreCompose}
	onPostcompose={handlePostCompose}
	onRendercomplete={handleRenderComplete}
>
	<Map.View />
</Map.Root>
Event HandlerTypeDescription
onPrecompose(evt: RenderEvent) => voidBefore layer composition
onPostcompose(evt: RenderEvent) => voidAfter layer composition
onRendercomplete(evt: RenderEvent) => voidRendering complete

Event Usage Examples

<script>
	function handleClick(evt) {
		const coordinate = evt.coordinate;
		console.log('Clicked at:', coordinate);
	}

	function handlePointerMove(evt) {
		// Update cursor or highlight features
		const pixel = evt.pixel;
		// Check for features at pixel
	}

	function handleMoveEnd(evt) {
		const map = evt.target;
		const view = map.getView();
		const center = view.getCenter();
		const zoom = view.getZoom();
		console.log('New view:', { center, zoom });
	}
</script>

<Map.Root
	onSingleclick={handleClick}
	onPointermove={handlePointerMove}
	onMoveend={handleMoveEnd}
>
	<Map.View />
	<!-- layers and other components -->
</Map.Root>

Map.View

Controls the map’s viewport including center, zoom, rotation, and projection.

Basic Usage

<script>
	import { Map } from 'svelte-openlayers';
</script>

<Map.Root>
	<Map.View center={[-74.0, 40.7]} zoom={10} rotation={0} />
</Map.Root>

Props

PropTypeDefaultDescription
center[number, number][0, 0]View center coordinates (bindable)
zoomnumber2Zoom level (bindable)
projectionProjectionLike'EPSG:3857'Map projection
minZoomnumber0Minimum zoom level
maxZoomnumber28Maximum zoom level
rotationnumber0View rotation in radians (bindable)
extentnumber[]undefinedConstraining extent
constrainRotationboolean | numbertrueConstrain rotation
enableRotationbooleantrueEnable rotation
onCenterChange(center: Coordinate) => voidundefinedCenter change callback
onZoomChange(zoom: number | undefined) => voidundefinedZoom change callback
onRotationChange(rotation: number) => voidundefinedRotation change callback
onMoveEnd(evt: any) => voidundefinedMove end event callback

Built-in Controls

Map.Root includes built-in controls that can be enabled/disabled via props. Custom control components are not yet implemented.

Available Controls

  • Zoom Control - Enable with zoomControl={true} (default: true)
  • Attribution Control - Enable with attributionControl={true} (default: true)
  • Rotate Control - Enable with rotateControl={true} (default: false)
  • Mouse Position Control - Enable with mousePositionControl={true} (default: false)

Control Configuration

<Map.Root
	zoomControl={true}
	attributionControl={true}
	rotateControl={false}
	mousePositionControl={true}
>
	<Map.View />
	<!-- layers -->
</Map.Root>