-
Notifications
You must be signed in to change notification settings - Fork 2k
/
app.js
129 lines (114 loc) · 2.71 KB
/
app.js
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
/* global document fetch window */
/* eslint-disable no-console */
import React, {PureComponent} from 'react';
import {render} from 'react-dom';
import DeckGL from 'deck.gl';
import {TileLayer} from '@deck.gl/geo-layers';
import {decodeTile} from './utils/decode';
// Set your mapbox token here
const MAPBOX_TOKEN = process.env.MapboxAccessToken; // eslint-disable-line
const INITIAL_VIEW_STATE = {
bearing: 0,
pitch: 0,
longitude: -122.45,
latitude: 37.78,
zoom: 12,
minZoom: 2,
maxZoom: 14,
height: window.innerHeight,
width: window.innerWidth
};
const MAP_LAYER_STYLES = {
stroked: false,
getLineColor: [192, 192, 192],
getFillColor: f => {
switch (f.properties.layer) {
case 'water':
return [140, 170, 180];
case 'landcover':
return [120, 190, 100];
default:
return [218, 218, 218];
}
},
getLineWidth: f => {
if (f.properties.layer === 'transportation') {
switch (f.properties.class) {
case 'primary':
return 12;
case 'motorway':
return 16;
default:
return 6;
}
}
return 1;
},
lineWidthMinPixels: 1,
getPointRadius: 100,
pointRadiusMinPixels: 2,
opacity: 1
};
function getTileData({x, y, z}) {
const mapSource = `https://a.tiles.mapbox.com/v4/mapbox.mapbox-streets-v7/${z}/${x}/${y}.vector.pbf?access_token=${MAPBOX_TOKEN}`;
return fetch(mapSource)
.then(response => {
return response.arrayBuffer();
})
.then(buffer => {
return decodeTile(x, y, z, buffer);
});
}
class Root extends PureComponent {
constructor(props) {
super(props);
this._onClick = this._onClick.bind(this);
this.state = {
clickedItem: null
};
}
_onClick(info) {
this.setState({clickedItem: info && info.object});
}
_renderClickedItem() {
const {clickedItem} = this.state;
if (!clickedItem || !clickedItem.properties) {
return null;
}
return (
<div
style={{
position: 'fixed',
zIndex: 9,
margin: '20px',
left: 0,
bottom: 0,
whiteSpace: 'nowrap',
pointerEvents: 'none'
}}
>
{JSON.stringify(clickedItem.properties)}
</div>
);
}
render() {
return (
<div>
<DeckGL
initialViewState={INITIAL_VIEW_STATE}
controller={true}
onLayerClick={this._onClick}
layers={[
new TileLayer({
...MAP_LAYER_STYLES,
pickable: true,
getTileData
})
]}
/>
{this._renderClickedItem()}
</div>
);
}
}
render(<Root />, document.body.appendChild(document.createElement('div')));