Svelte Maplibre

API / source.ts

Source Helpers

Functions that add and remove map sources safely. These are useful when you write your own source components.

Functions

addSource function

import { addSource } from 'svelte-maplibre';

Add a source to the map.

function addSource(map: Map, sourceId: string, source: SourceSpecification, okToAdd: (sourceId: string) => boolean, cb: () => void): void;

Parameters

  • map — The map instance
  • sourceId — The ID of the source to add
  • source — The source specification object
  • okToAdd — Callback to check if the source should still be added
  • cb — Callback when the source has been added This properly handles the case where an old source with the same ID is still being removed.

removeSource function

import { removeSource } from 'svelte-maplibre';

A helper function that removes a source from the map after all of the layers inside it have had a chance to remove themselves.

function removeSource(map: Map, sourceId: string, sourceObj: unknown): void;

Parameters

  • mapStore — The store containing the Map instance
  • sourceId — The ID of the source to remove
  • sourceObj — The source object that was originally added Waits one tick to ensure layers have a chance to be removed, then checks if the source with the given ID is still the same object as was originally added. If so, it removes the source from the map. This avoids removing a source that was already replaced by another source reusing the same ID.