-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathmaplibre.ts
More file actions
98 lines (86 loc) · 2.61 KB
/
Copy pathmaplibre.ts
File metadata and controls
98 lines (86 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// deck.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import {MapboxOverlay} from '@deck.gl/mapbox';
import maplibregl from 'maplibre-gl';
import type {Config} from '../../types';
import {getBaseMapViewState} from '../../config';
export function mount(container: HTMLElement, config: Config): () => void {
const {
mapStyle,
initialViewState,
layers,
interleaved,
globe,
multiView,
views,
layerFilter,
useDevicePixels,
onViewStateChange
} = config;
// For multi-view, extract the mapbox view state for the base map
const mapInitialViewState = getBaseMapViewState(initialViewState);
const mapOpts: maplibregl.MapOptions = {
container,
style: mapStyle,
center: [mapInitialViewState.longitude, mapInitialViewState.latitude],
zoom: mapInitialViewState.zoom,
bearing: mapInitialViewState.bearing || 0,
pitch: mapInitialViewState.pitch || 0
};
if (typeof useDevicePixels === 'number') {
mapOpts.pixelRatio = useDevicePixels;
} else if (useDevicePixels === false) {
mapOpts.pixelRatio = 1;
}
const map = new maplibregl.Map(mapOpts);
const overlayConfig: any = {
interleaved,
layers,
useDevicePixels
};
if (multiView && views) {
overlayConfig.views = views;
overlayConfig.initialViewState = initialViewState;
}
if (multiView && layerFilter) {
overlayConfig.layerFilter = layerFilter;
}
const deckOverlay = new MapboxOverlay(overlayConfig);
let cancelled = false;
map.on('load', () => {
if (cancelled) return;
// Set projection before adding overlay (critical for globe + interleaved mode)
if (globe) {
map.setProjection({type: 'globe'} as any);
// Re-apply center/zoom after projection change (setProjection resets to 0,0)
map.setCenter([mapInitialViewState.longitude, mapInitialViewState.latitude]);
map.setZoom(mapInitialViewState.zoom);
// Wait for projection to be fully applied before adding overlay
requestAnimationFrame(() => {
if (cancelled) return;
map.addControl(deckOverlay as any);
map.addControl(new maplibregl.NavigationControl());
});
} else {
map.addControl(deckOverlay as any);
map.addControl(new maplibregl.NavigationControl());
}
});
if (onViewStateChange) {
map.on('move', () => {
const center = map.getCenter();
onViewStateChange({
latitude: center.lat,
longitude: center.lng,
zoom: map.getZoom(),
bearing: map.getBearing(),
pitch: map.getPitch()
});
});
}
return () => {
cancelled = true;
map.remove();
};
}