-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathmapbox.ts
More file actions
87 lines (74 loc) · 2.17 KB
/
Copy pathmapbox.ts
File metadata and controls
87 lines (74 loc) · 2.17 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
// deck.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import {MapboxOverlay} from '@deck.gl/mapbox';
// eslint-disable-next-line import/default
import mapboxgl from 'mapbox-gl';
import type {Config} from '../../types';
import {getBaseMapViewState} from '../../config';
export function mount(container: HTMLElement, config: Config): () => void {
const {
mapStyle,
initialViewState,
layers,
interleaved,
multiView,
views,
layerFilter,
useDevicePixels,
onViewStateChange
} = config;
// eslint-disable-next-line no-process-env
const mapboxToken = process.env.MapboxAccessToken;
if (!mapboxToken) {
container.innerHTML = `
<div style="padding: 20px; color: red; background-color: #ffebee; height: 100%;">
<h3>Mapbox Configuration Required</h3>
<p>Set MapboxAccessToken environment variable.</p>
</div>
`;
return () => {};
}
(mapboxgl as any).accessToken = mapboxToken;
// For multi-view, extract the mapbox view state for the base map
const mapInitialViewState = getBaseMapViewState(initialViewState);
const mapOpts: mapboxgl.MapOptions = {
container,
style: mapStyle,
center: [mapInitialViewState.longitude, mapInitialViewState.latitude],
zoom: mapInitialViewState.zoom,
bearing: mapInitialViewState.bearing || 0,
pitch: mapInitialViewState.pitch || 0
};
const map = new mapboxgl.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);
map.addControl(deckOverlay as any);
map.addControl(new mapboxgl.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 () => {
map.remove();
};
}