-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMap.jsx
More file actions
138 lines (124 loc) · 3.65 KB
/
Map.jsx
File metadata and controls
138 lines (124 loc) · 3.65 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import React from 'react';
import { StaticMap } from 'react-map-gl';
import DeckGL from '@deck.gl/react';
import { MapView } from '@deck.gl/core';
import ZoomIn from '../assets/add.svg?react';
import ZoomOut from '../assets/negative.svg?react';
import Center from '../assets/reticle.svg?react';
import iconMapping from '../assets/location-icon-mapping.json'
import iconAtlas from '../assets/location-icon-atlas.png'
import { renderContextMenu } from './ContextMenu';
import IconClusterLayer from './IconClusterLayer';
import { renderTooltip } from './Tooltip';
const MAP_VIEW = new MapView({ repeat: true });
const INITIAL_VIEW_STATE = {
latitude: 37,
longitude: -95.7,
zoom: 1.8,
minZoom: 0,
maxZoom: 20,
pitch: 0,
bearing: 0
};
const ZOOM_STEP = 0.5;
const DEFAULT_SIZE_SCALE = 40;
const MAP_STYLE = 'mapbox://styles/mapbox/light-v1';
const MAPBOX_TOKEN =
'pk.eyJ1IjoiamZyYW50eSIsImEiOiJjam91bzF2YWUxZTFzM3FydnBncWs3dnoyIn0.cXRBg3Vcetu9d-gjstnGig';
function getFeatures() {
return {
mapboxStyle: MAP_STYLE,
mapboxToken: MAPBOX_TOKEN,
};
}
function Map(props) {
const { data, sizeScale } = props;
const [viewState, setViewState] = React.useState({...INITIAL_VIEW_STATE});
const [hoverInfo, setHoverInfo] = React.useState(null);
const [contextMenuInfo, setContextMenuInfo] = React.useState(null);
const hideTooltip = () => {
setHoverInfo(null);
setContextMenuInfo(null);
};
const handleClick = (info, event) => {
if (event.rightButton) {
setHoverInfo(null);
setContextMenuInfo(info.picked ? info : null);
} else {
setContextMenuInfo(null);
setHoverInfo(info.picked ? info : null);
}
};
const adjustZoom = (delta) => {
setViewState(viewState => {
return {
...viewState,
zoom: Math.max(
viewState.minZoom,
Math.min(viewState.maxZoom, viewState.zoom + delta),
),
}
});
}
const layer = new IconClusterLayer({
id: 'icon-cluster',
data,
pickable: true,
getPosition: d => d.coordinates,
iconAtlas,
iconMapping,
onHover: contextMenuInfo == null &&
!hoverInfo?.objects && // when click to expand tooltip
setHoverInfo,
sizeScale: sizeScale ?? DEFAULT_SIZE_SCALE,
sizeMinPixels: 1,
sizeMaxPixels: 100,
})
const features = getFeatures();
return (
<div onContextMenu={e => {
e.preventDefault();
e.stopPropagation();
}}>
<DeckGL
layers={[layer]}
views={MAP_VIEW}
viewState={viewState}
controller={{ dragRotate: false }}
onViewStateChange={({ viewState }) => {
setViewState(viewState);
hideTooltip();
}}
onClick={handleClick}
>
<StaticMap
key={features.mapboxToken}
reuseMaps
mapboxApiAccessToken={features.mapboxToken}
// mapStyle={features.mapboxStyle}
preventStyleDiffing
/>
{renderTooltip(hoverInfo)}
{renderContextMenu(contextMenuInfo)}
</DeckGL>
{/* icons */}
<section className="iconsSection">
<div className="icons">
<button title="Center data" onClick={() => setViewState({ ...INITIAL_VIEW_STATE })}>
<Center />
</button>
</div>
<div className="icons" style={{ marginTop: '8px' }}>
<button title="Zoom in" onClick={() => adjustZoom(ZOOM_STEP)}>
<ZoomIn />
</button>
<div className="divider"></div>
<button title="Zoom out" onClick={() => adjustZoom(-1 * ZOOM_STEP)}>
<ZoomOut />
</button>
</div>
</section>
</div>
);
}
export default Map;