-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmap2gl.html
More file actions
297 lines (271 loc) · 11.1 KB
/
map2gl.html
File metadata and controls
297 lines (271 loc) · 11.1 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Map 2</title>
<script src='https://api.mapbox.com/mapbox-gl-js/v0.21.0/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v0.21.0/mapbox-gl.css' rel='stylesheet'/>
<script src="js/jquery-3.0.0.min.js"></script>
<script src="js/leaflet.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id='map'>
<style>
#map {
height: 98vh;
}
</style>
</div>
<nav id='filter-group' class='filter-group'></nav>
<style>
.filter-group {
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
font-weight: 600;
position: absolute;
top: 30px;
right: 20px;
z-index: 1;
border-radius: 3px;
width: 200px;
color: #fff;
}
.filter-group input[type=checkbox]:first-child + label {
border-radius: 3px 3px 0 0;
}
.filter-group label:last-child {
border-radius: 0 0 3px 3px;
border: none;
}
.filter-group input[type=checkbox] {
display: none;
}
.filter-group input[type=checkbox] + label {
background-color: #3386c0;
display: block;
cursor: pointer;
padding: 10px;
border-bottom: 1px solid rgba(0, 0, 0, 0.25);
}
.filter-group input[type=checkbox] + label {
background-color: #3386c0;
text-transform: capitalize;
}
.filter-group input[type=checkbox] + label:hover,
.filter-group input[type=checkbox]:checked + label {
background-color: #4ea0da;
}
.filter-group input[type=checkbox]:checked + label:before {
content: '✔';
margin-right: 5px;
}
</style>
<script>
String.prototype.trunc = String.prototype.trunc ||
function (n) {
return (this.length > n) ? this.substr(0, n - 1) + '…' : this;
};
mapboxgl.accessToken = 'pk.eyJ1IjoibWFydmltb3RvIiwiYSI6ImNpcHM1bnpnZTAwMTNodW0ycDc1cm5idDMifQ.AY9BP_AH0jplhx94YQ1ePA';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/marvimoto/cipz5r8kc0006erm4812edrqx',
center: [10, 51],
zoom: 6
});
var filterGroup = document.getElementById('filter-group');
var firstElementReady = false;
var loadstart = Date.now();
console.time('Visualisierung');
console.timeStamp('Beginn der Visualisierung');
var url = '/api/public/json/geojson';
var geosource = new mapboxgl.GeoJSONSource({
data: url,
cluster: true,
clusterMaxZoom: 10,
clusterRadius: 100
});
map.on('load', function () {
map.addSource("markers", geosource);
// Display the earthquake data in three layers, each filtered to a range of
// count values. Each range gets a different fill color.
var layers = [
[0, '#ff1a1a']
];
window.setTimeout(function () {
var places = $.getJSON(url, function (places) {
places.features.forEach(function (feature) {
var symbol = feature.properties['icon'];
var layerID = 'layer-' + symbol;
if (!map.getLayer(layerID)) {
map.addLayer({
"id": layerID,
"type": "symbol",
"source": "markers",
"layout": {
"icon-image": symbol,
"icon-allow-overlap": true
},
"filter": ["==", "icon", symbol]
})
var input = document.createElement('input');
input.type = 'checkbox';
input.id = layerID;
input.checked = true;
filterGroup.appendChild(input);
var label = document.createElement('label');
label.setAttribute('for', layerID);
label.textContent = symbol;
filterGroup.appendChild(label);
input.addEventListener('change', function (e) {
var newmarkers = L.geoJson(places, {
filter: function (feature, layer) {
return feature.properties.symbol != input.id;
}
})
console.log(newmarkers.toGeoJSON());
map.getSource('markers').setData(newmarkers.toGeoJSON());
})
}
})
});
}, 2000);
layers.forEach(function (layer, i) {
map.addLayer({
"id": "cluster",
"type": "circle",
"source": "markers",
"paint": {
"circle-color": "#ff1a1a",
"circle-radius": 18
},
"filter": i === 0 ?
[">=", "point_count", layer[0]] :
["all",
[">=", "point_count", layer[0]],
["<", "point_count", layers[i - 1][0]]]
});
});
// Add a layer for the clusters' count labels
map.addLayer({
"id": "cluster-count",
"type": "symbol",
"source": "markers",
"layout": {
"text-field": "{point_count}",
"text-font": [
"DIN Offc Pro Medium",
"Arial Unicode MS Bold"
],
"text-size": 16
},
"paint": {
"text-color": "#ffffff"
}
});
map.on('click', function (e) {
var features = map.queryRenderedFeatures(e.point, {layers: ['unclustered-points', 'cluster']});
if (!features.length) {
return;
}
var feature = features[0];
if (feature.layer.id == 'unclustered-points') {
// Populate the popup and set its coordinates
// based on the feature found.
var popup = new mapboxgl.Popup()
.setLngLat(feature.geometry.coordinates)
.setHTML(feature.properties.description.trunc(500))
.addTo(map);
}
else {
map.flyTo({
center: feature.geometry.coordinates,
zoom: map.getZoom() + 2
})
}
;
});
map.on('mousemove', function (e) {
var clusters = map.queryRenderedFeatures(e.point, {layers: ['unclustered-points', 'cluster']});
map.getCanvas().style.cursor = (clusters.length) ? 'pointer' : '';
});
map.on("render", function () {
var markers = map.querySourceFeatures('markers', {});
for (var i = 0; i < markers.length; i++) {
if (typeof(markers[i].properties.cluster) == 'undefined') {
console.log(markers[i]);
}
}
var markercount = 0;
var points = map.queryRenderedFeatures({layers: ['cluster']});
if (points.length != 0) {
for (var i = 0; i < points.length; i++) {
markercount += points[i].properties.point_count;
}
if (markercount == 19267) {
console.timeEnd('Visualisierung');
console.timeStamp('Ende der Visualisierung');
console.log('Fertig');
// features.forEach(function(feature){
// console.log(feature);
// })
}
;
}
});
})
// function visualize(path) {
// console.profile('Visualisierung');
// console.log(Date.now() - loadstart + "ms: Json wird geladen " + path);
// var json = $.getJSON(path, function (json) {
// var count = Object.keys(json).length;
// start = Date.now();
// var markers = [];
// for (var i=0; i<count; i++) {
// //Koordinaten auslesen
// var data = json[i];
// if (typeof data.allLocations != 'undefined'){
// var coord = data.allLocations[0].split(",");
// var x = coord[0];
// var y = coord[1];
//// var eventIcon = L.Icon.extend({
//// options: {
//// iconSize: [32, 32]
//// }
//// });
//// switch(data.eventType[0]) {
//// case "Behinderungen": var thisIcon = new eventIcon({iconUrl: 'images/traffic-events/icon_traffic-event_roadblock.svg'});
//// break;
//// case "Stau": var thisIcon = new eventIcon({iconUrl: 'images/traffic-events/icon_traffic-event_trafficjam.svg'});
//// break;
//// case "Schienenersatzverkehr": var thisIcon = new eventIcon({iconUrl: 'images/traffic-events/icon_traffic-event_replacement.svg'});
//// break;
//// case "Verzoegerung": var thisIcon = new eventIcon({iconUrl: 'images/traffic-events/icon_traffic-event_delay.svg'});
//// break;
//// case "Unfall": var thisIcon = new eventIcon({iconUrl: 'images/traffic-events/icon_traffic-event_accident.svg'});
//// break;
//// default: var thisIcon = new eventIcon({iconUrl: 'images/traffic-events/icon_traffic-event_weather.svg'});
//// }
// var marker = new mapboxgl.Marker()
// .setLngLat(coord)
// .addTo(map);
//// var marker = L.marker([x,y], {icon: thisIcon});
//// marker.bindPopup("<b>" + data.title + "</b><br>" + data.text + "<br>");
//// marker.addTo(mymap);
// //Marker clustern
// }
// }
// //Cluster zur Map hinzufügen
// if (!firstElementReady) {
// console.log(Date.now() - loadstart + 'ms: Erster Satz gerendert')
// firstElementReady = true;
// }
// console.log(Date.now() - loadstart + "ms: Json geladen " + path);
// var stop = Date.now();
// console.timeEnd('Visualisierung');
// console.timeStamp('Abschluss der Visualisierung');
// console.profileEnd();
// });
// }
</script>
</body>
</html>