63 lines
1.4 KiB
TypeScript
63 lines
1.4 KiB
TypeScript
|
|
import maplibregl, {
|
||
|
|
type GeoJSONSourceSpecification,
|
||
|
|
type LayerSpecification,
|
||
|
|
} from 'maplibre-gl';
|
||
|
|
|
||
|
|
export function ensureGeoJsonSource(
|
||
|
|
map: maplibregl.Map,
|
||
|
|
sourceId: string,
|
||
|
|
data: GeoJSON.GeoJSON,
|
||
|
|
) {
|
||
|
|
const existing = map.getSource(sourceId);
|
||
|
|
if (existing) {
|
||
|
|
(existing as maplibregl.GeoJSONSource).setData(data);
|
||
|
|
} else {
|
||
|
|
map.addSource(sourceId, {
|
||
|
|
type: 'geojson',
|
||
|
|
data,
|
||
|
|
} satisfies GeoJSONSourceSpecification);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function ensureLayer(
|
||
|
|
map: maplibregl.Map,
|
||
|
|
spec: LayerSpecification,
|
||
|
|
options?: { before?: string },
|
||
|
|
) {
|
||
|
|
if (map.getLayer(spec.id)) return;
|
||
|
|
const before = options?.before && map.getLayer(options.before) ? options.before : undefined;
|
||
|
|
map.addLayer(spec, before);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function setLayerVisibility(map: maplibregl.Map, layerId: string, visible: boolean) {
|
||
|
|
if (!map.getLayer(layerId)) return;
|
||
|
|
try {
|
||
|
|
map.setLayoutProperty(layerId, 'visibility', visible ? 'visible' : 'none');
|
||
|
|
} catch {
|
||
|
|
// ignore
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function cleanupLayers(
|
||
|
|
map: maplibregl.Map,
|
||
|
|
layerIds: string[],
|
||
|
|
sourceIds: string[],
|
||
|
|
) {
|
||
|
|
requestAnimationFrame(() => {
|
||
|
|
for (const id of layerIds) {
|
||
|
|
try {
|
||
|
|
if (map.getLayer(id)) map.removeLayer(id);
|
||
|
|
} catch {
|
||
|
|
// ignore
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for (const id of sourceIds) {
|
||
|
|
try {
|
||
|
|
if (map.getSource(id)) map.removeSource(id);
|
||
|
|
} catch {
|
||
|
|
// ignore
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|