-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapbox-accidentes.html
209 lines (161 loc) · 6.07 KB
/
mapbox-accidentes.html
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
<html>
<head>
<meta charset='utf-8' />
<title>Accidentes BCN</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.52.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.52.0/mapbox-gl.css' rel='stylesheet' />
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
height: 100%
}
.legend {
background-color: #fff;
border-radius: 3px;
bottom: 40px;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
padding: 10px;
position: absolute;
right: 10px;
z-index: 1;
}
.legend h4 {
margin: 0 0 10px;
}
.legend p {
margin-left: 30px;
position: absolute;
display: block;
top: 0;
}
.legend div {
position: relative;
}
.legend div span {
border-radius: 50%;
display: inline-block;
margin-right: 5px;
opacity: 0.9;
background-color: #d49a66;
}
.map-overlay {
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
position: absolute;
width: 25%;
top: 0;
left: 0;
padding: 10px;
z-index: 100;
}
.map-overlay .map-overlay-inner {
background-color: #fff;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.20);
border-radius: 3px;
padding: 10px;
margin-bottom: 10px;
}
.map-overlay input {
background-color: transparent;
display: inline-block;
width: 100%;
position: relative;
margin: 0;
cursor: ew-resize;
}
</style>
<script>
function Init() {
var diasSemana = ["Dilluns", "Dimarts", "Dimecres", "Dijous", "Divendres", "Dissabte", "Diumenge"];
var colorDia = ['#e26131', '#d0d378', '#78d3b0', '#78bcd3', '#9e78d3', '#d378cd', '#d37878'];
mapboxgl.accessToken =
'pk.eyJ1IjoibWFyY3JiMyIsImEiOiJjanJqanZvMXowYzdnNDRsbGN4N3JyMGhuIn0.im2TOkqyqEpCXhN4LWoZuA';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/marcrb3/cjrql9wgldozd2toc9yhs4362',
center: [2.16859, 41.3954],
zoom: 13,
hash: true,
pitch: 45,
attributionControl: false
});
map.addControl(new mapboxgl.AttributionControl({ compact: true }));
map.addControl(new mapboxgl.NavigationControl());
creaLeyenda();
map.on('load', function () {
map.addSource("accidentes_source", {
type: "vector",
url: "mapbox://marcrb3.dz39y63t"
}); //fin map source
map.addLayer({
id: "accidentes",
type: "circle",
source: "accidentes_source",
'source-layer': "accidents17-bzfbtc",
paint: {
'circle-radius': 4,
'circle-color': {
property: "Descripcio_dia_setmana",
type: 'categorical',
stops: [
[diasSemana[0], colorDia[0]],
[diasSemana[1], colorDia[1]],
[diasSemana[2], colorDia[2]],
[diasSemana[3], colorDia[3]],
[diasSemana[4], colorDia[4]],
[diasSemana[5], colorDia[5]],
[diasSemana[6], colorDia[6]]
]
},
'circle-opacity': 0.9
}
, filter: ['==', 'Descripcio_dia_setmana', diasSemana[0]]
}); //fin add layers
document.getElementById('slider').addEventListener('input', function (e) {
filtraDias(e.target.value);
});
}); //Fin load mapa
function creaLeyenda() {
var leyendaHtml = "<div><b>Dia Setmana</b></div>";
for (i = 0; i < diasSemana.length; i++) {
leyendaHtml = leyendaHtml + '<div><span style="background-color:' + colorDia[i] +
';width:10px;height:10px;margin: 0 5px"></span>' + diasSemana[i] + '</div>';
}
document.getElementById('legend').innerHTML = leyendaHtml;
}
function filtraDias(numdia) {
var filters = ['==', 'Descripcio_dia_setmana', diasSemana[numdia]];
if(numdia == 7){
filters = ['!=', 'Descripcio_dia_setmana',""];
map.setFilter('accidentes', filters);
document.getElementById('diasemanaid').textContent = "Tots";
}else{
map.setFilter('accidentes', filters);
document.getElementById('diasemanaid').textContent = diasSemana[numdia];
}
}
}
</script>
</head>
<body onload="Init()">
<div id='legend' class='legend'>
### Accidentes
</div>
<div class='map-overlay top'>
<div class='map-overlay-inner'>
Accidents per dies de la setmana:
<label id='diasemanaid'></label>
<input id='slider' type='range' min='0' max='7' step='1' value='0' />
</div>
</div>
<div id='map'></div>
</body>
</html>