-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
197 lines (177 loc) · 4.78 KB
/
index.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { CREDENTIALS } from './credentials';
import mapboxgl from 'mapbox-gl';
import MapboxGeocoder from '@mapbox/mapbox-gl-geocoder';
mapboxgl.accessToken = CREDENTIALS.mapboxgl;
const map = new mapboxgl.Map({
container: 'map',
// style: 'mapbox://styles/mapbox/outdoors-v9',
style: 'mapbox://styles/mapbox/dark-v9',
// style: 'mapbox://styles/concept3d/cjkvhp2eu2na72sqque0frweq',
center: [-122.42347, 37.75846],
zoom: 12.47,
hash: true
});
const formatGeoJson = collection => {
collection.features.forEach(feature => {
feature.geometry = {
type: 'Point',
coordinates: [feature.properties.longitude, feature.properties.latitude]
};
feature.properties['marker-color'] = '#0000ff';
feature.properties.line = 'blue';
});
return collection;
};
const loadMap = () => {
controls();
hillRgb();
extrusions();
fetchGlassAndRenderMarkers();
};
map.on('load', () => loadMap());
map.on('zoom', () => ease());
const controls = () => {
map.addControl(
new MapboxGeocoder({ accessToken: mapboxgl.accessToken }),
'top-right'
);
map.addControl(new mapboxgl.NavigationControl(), 'top-right');
};
const hillRgb = () => {
map.addSource('dem', {
type: 'raster-dem',
url: 'mapbox://mapbox.terrain-rgb'
});
map.addLayer(
{
id: 'hillshading',
source: 'dem',
type: 'hillshade'
}
// 'waterway-river-canal-shadow'
);
};
const extrusions = () => {
const la = map.getStyle().layers;
const layers = la.filter(l => l.type === 'symbol' && l.layout['text-field']);
const layerId = layers[0].id;
map.addLayer(
{
id: '3d-buildings',
source: 'composite',
'source-layer': 'building',
filter: ['==', 'extrude', 'true'],
type: 'fill-extrusion',
minzoom: 12,
paint: {
'fill-extrusion-color': '#FFFFFF',
'fill-extrusion-height': [
'interpolate',
['linear'],
['zoom'],
15,
0,
15.05,
['get', 'height']
],
'fill-extrusion-base': [
'interpolate',
['linear'],
['zoom'],
15,
0,
15.05,
['get', 'min_height']
],
'fill-extrusion-opacity': 0.6
}
},
layerId
);
};
const ease = () => {
map.getZoom() > 16
? map.easeTo({ pitch: 40, bearing: -17 })
: map.easeTo({ pitch: 0, bearing: 0 });
};
async function fetchGlassAndRenderMarkers() {
const glassData = await fetch(
'https://data.sfgov.org/resource/aqnm-j7pg.geojson?$where=_252_broken_glass_y_n_where_y_none >= 0&$limit=10000'
);
let jason = await glassData.json();
let newnew = formatGeoJson(jason);
map.addSource('glass', {
type: 'geojson',
data: newnew,
cluster: true,
clusterMaxZoom: 13, // Max zoom to cluster points on
clusterRadius: 20 // Radius of each cluster when clustering points (defaults to 50)
});
map.addLayer({
id: 'clusters',
type: 'circle',
source: 'glass',
filter: ['has', 'point_count'],
paint: {
// * Blue, 20px circles when point count is less than 100
// * Yellow, 30px circles when point count is between 100 and 750
// * Pink, 40px circles when point count is greater than or equal to 750
'circle-color': [
'step',
['get', 'point_count'],
'#51bbd6',
50,
'#f1f075',
200,
'#f28cb1'
],
'circle-radius': ['step', ['get', 'point_count'], 20, 100, 30, 750, 40]
}
});
map.addLayer({
id: 'cluster-count',
type: 'symbol',
source: 'glass',
filter: ['has', 'point_count'],
layout: {
'text-field': '{point_count_abbreviated}',
'text-font': ['DIN Offc Pro Medium', 'Arial Unicode MS Bold'],
'text-size': 12
}
});
map.loadImage(
'https://cdn.shopify.com/s/files/1/1061/1924/products/Glasses_Emoji_large.png?v=1571606064',
function(err, image) {
map.addImage('glass', image);
map.addLayer({
id: 'unclustered-point',
type: 'symbol',
source: 'glass',
filter: ['!', ['has', 'point_count']],
layout: {
'icon-image': 'glass',
'icon-size': 0.05
}
});
}
);
map.on('click', 'clusters', function(e) {
var features = map.queryRenderedFeatures(e.point, { layers: ['clusters'] });
var clusterId = features[0].properties.cluster_id;
map
.getSource('glass')
.getClusterExpansionZoom(clusterId, function(err, zoom) {
if (err) return;
map.easeTo({
center: features[0].geometry.coordinates,
zoom: zoom
});
});
});
map.on('mouseenter', 'clusters', function() {
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', 'clusters', function() {
map.getCanvas().style.cursor = '';
});
}