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 instancesourceId— The ID of the source to addsource— The source specification objectokToAdd— Callback to check if the source should still be addedcb— 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 instancesourceId— The ID of the source to removesourceObj— 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.