Layer Components

Layers are the foundation for displaying different types of data on your map. Svelte OpenLayers provides components for raster tiles, vector data, and more.

Layer.Tile

Displays raster tile data from various sources like OpenStreetMap, satellite imagery, or custom tile servers.

Basic Usage

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

<Map.Root>
	<Map.View center={[0, 0]} zoom={2} />
	<Layer.Tile source="osm" />
</Map.Root>

Props

PropTypeDefaultDescription
source'osm' | 'xyz' | Source'osm'Tile source configuration
urlstringundefinedURL for XYZ source
opacitynumber1Layer opacity (0-1)
visiblebooleantrueLayer visibility
zIndexnumberundefinedLayer stacking order
minZoomnumberundefinedMinimum zoom level
maxZoomnumberundefinedMaximum zoom level
preloadnumber0Preload tiles
layerTileLayer | nullnullBindable layer instance (read-only)
attributionsstring | string[]undefinedLayer attributions
crossOriginstring | nullundefinedCross-origin setting

Built-in Sources

<!-- OpenStreetMap -->
<Layer.Tile source="osm" />

<!-- Custom XYZ source -->
<Layer.Tile
	source="xyz"
	url="https://{a - c}.tile.example.com/{z}/{x}/{y}.png"
	attributions="© Example"
/>

<!-- Using OpenLayers source directly -->
<Layer.Tile source={customTileSource} />

Note: Currently only ‘osm’ and ‘xyz’ string sources are supported. For other sources like satellite imagery, you’ll need to create OpenLayers source instances directly.

Dynamic Layer Control

<script>
	let showSatellite = false;
	let layerOpacity = 1;
</script>

<Map.Root>
	<Map.View />
	<Layer.Tile source="osm" />

	{#if showSatellite}
		<Layer.Tile source="esri-satellite" opacity={layerOpacity} />
	{/if}
</Map.Root>

<div class="controls">
	<label>
		<input type="checkbox" bind:checked={showSatellite} />
		Show Satellite
	</label>

	<label>
		Opacity:
		<input type="range" min="0" max="1" step="0.1" bind:value={layerOpacity} />
	</label>
</div>

Layer.Vector

Displays vector data like points, lines, and polygons. Container for Feature components.

Basic Usage

<Map.Root>
	<Map.View />
	<Layer.Tile source="osm" />
	<Layer.Vector>
		<Feature.Point coordinates={[0, 0]} />
		<Feature.LineString
			coordinates={[
				[0, 0],
				[10, 10]
			]}
		/>
	</Layer.Vector>
</Map.Root>

Props

PropTypeDefaultDescription
opacitynumber1Layer opacity (0-1)
visiblebooleantrueLayer visibility
zIndexnumberundefinedLayer stacking order
minZoomnumberundefinedMinimum zoom level
maxZoomnumberundefinedMaximum zoom level
styleStyleLikeundefinedDefault style for features
updateWhileAnimatingbooleanfalseUpdate during animations
updateWhileInteractingbooleanfalseUpdate during interactions
renderBuffernumber100Render buffer in pixels
layerVectorLayer | nullnullBindable layer instance (read-only)
sourceVectorSource | nullnullBindable source instance (read-only)

Vector Layer with Styling

<script>
	const vectorStyle = {
		fill: { color: 'rgba(255, 0, 0, 0.3)' },
		stroke: { color: 'red', width: 2 },
		circle: { radius: 6, fill: { color: 'blue' } }
	};
</script>

<Map.Root>
	<Map.View />
	<Layer.Tile source="osm" />
	<Layer.Vector style={vectorStyle}>
		<!-- Features inherit the layer style -->
		<Feature.Point coordinates={[0, 0]} />
		<Feature.Polygon
			coordinates={[
				[
					[0, 0],
					[10, 0],
					[10, 10],
					[0, 10],
					[0, 0]
				]
			]}
		/>
	</Layer.Vector>
</Map.Root>

Coming Soon: Additional layer types including Layer.VectorTile and Layer.Image are planned for future releases.