-
Notifications
You must be signed in to change notification settings - Fork 2
/
map.html
272 lines (238 loc) · 9.71 KB
/
map.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
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
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Carburants</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.3/leaflet.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.3/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.10.3/math.min.js"></script>
<link rel="stylesheet" href="./dist/MarkerCluster.css" />
<link rel="stylesheet" href="./dist/MarkerCluster.Default.css" />
<script src="./dist/leaflet.markercluster-src.js"></script>
<script src="./json/address_points.js"></script>
<meta charset=utf-8>
<style>
#map{ width: 100%; height: 100%; }
table{
font-size: small;
border-spacing: 10px 2px;
}
.thupdate{
white-space: nowrap;
}
</style>
<script type="text/javascript">
// Function to update the URI with parameters
function UpdateQueryString(key, value, url) {
if (!url) url = window.location.href;
var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"),
hash;
if (re.test(url)) {
if (typeof value !== 'undefined' && value !== null) {
return url.replace(re, '$1' + key + "=" + value + '$2$3');
}
else {
hash = url.split('#');
url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
if (typeof hash[1] !== 'undefined' && hash[1] !== null) {
url += '#' + hash[1];
}
return url;
}
}
else {
if (typeof value !== 'undefined' && value !== null) {
var separator = url.indexOf('?') !== -1 ? '&' : '?';
hash = url.split('#');
url = hash[0] + separator + key + '=' + value;
if (typeof hash[1] !== 'undefined' && hash[1] !== null) {
url += '#' + hash[1];
}
return url;
}
else {
return url;
}
}
}
// A custom Control to show a checkbox
L.Control.CheckBox = L.Control.extend({
options: {
position: 'topleft',
checked: false,
name: 'checkbox',
label: 'CheckBox'
},
initialize: function(options) {
L.Util.setOptions(this, options)
},
onAdd: function(map) {
var div = L.DomUtil.create('div', 'leaflet-control-layers leaflet-control-layers-expanded');
var input = L.DomUtil.create('input', '', div);
input.type = 'checkbox';
input.checked = this.options.checked;
var label = L.DomUtil.create('span', '', div);
label.innerHTML = this.options.label;
L.DomEvent.disableClickPropagation(div);
L.DomEvent.disableScrollPropagation(div);
L.DomEvent.on(input, 'click', function() { input.checked ? map.fire('onCheckbox-' + this.options.name) : map.fire('offCheckbox-' + this.options.name); }, this);
return div;
},
});
L.control.checkBox = function(opts) {
return new L.Control.CheckBox(opts);
}
// Create icons for markers
var iconColors = ['blue', 'green', 'yellow', 'orange', 'red', 'black'];
var icons = {};
for (var i = 0; i < iconColors.length; i++) {
icons[i] = L.icon({
iconUrl: './images/marker-icon-'+iconColors[i]+'.png',
shadowUrl: './images/marker-shadow.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
shadowSize: [41, 41]
});
}
</script>
</head>
<body>
<!-- Fork me on github -->
<a href="https://github.com/Hatrix42/opencarbumap"><img style="position: absolute; top: 0; right: 0; border: 0; z-index:1000;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a>
<div id="map"></div>
<script type="text/javascript">
function findCurrentLayer(map, overlayMaps) {
for (var name in overlayMaps) {
if (map.hasLayer(overlayMaps[name])) {
return overlayMaps[name];
}
}
}
function colorizeMarkers(layer, dynamic, bounds) {
var visibleMarkers = [];
layer.eachLayer(function(marker) {
if (!dynamic || bounds.contains(marker.getLatLng())) {
visibleMarkers.push(marker);
}
});
// Get all prices for a specific fuel
var prices = [...visibleMarkers].map(x => x.prices[layer.name]);
// Get the 6 quantiles for this range of prices
var quantiles = math.quantileSeq(prices, [...[...Array(5).keys()].map(x => (x+1) / 6)]);
// Set the color following quantiles
for (var marker of visibleMarkers) {
var setted = false;
for (var index in quantiles) {
if (marker.prices[layer.name] <= quantiles[index]) {
marker.setIcon(icons[index]);
setted = true;
break;
}
}
if (!setted) marker.setIcon(icons[5]);
}
}
function initializeMarkers(map, fuel_list) {
// Initialize Layers for each fuel
var overlayMaps = {};
for (var idx in fuel_list) {
var markers = L.markerClusterGroup({
removeOutsideVisibleBounds: true,
disableClusteringAtZoom: 11,
maxClusterRadius: 60
});
markers.name = fuel_list[idx];
overlayMaps[fuel_list[idx]] = markers;
}
// Create markers with pop-up
for (var index in addressPoints) {
var lat = addressPoints[index][0];
var lng = addressPoints[index][1];
var city = addressPoints[index][2];
var prices = addressPoints[index][3];
var brand = addressPoints[index][4];
var remark = addressPoints[index][5]
var title;
// Check brand
if (brand) {
title = '<b>' + brand + '</b>' + '<br>' + city;
} else {
title = city;
}
title += "<br>";
// Check remark
remark = remark ? remark : "";
// Create a table for the prices, so it's more pleasant to look at
title += "<table>"
title += "<thead><tr>"
if (remark)
title += "<th>Type</th><th>Prix</th><th class='thupdate' >Dernière MàJ</th>"
else
title += "<th>Type</th><th>Prix</th>"
title += "</tr></thead>"
title += "<tbody>"
for (var fuel_name in prices) {
title += "<tr><td>" + fuel_name + "</td><td>" + prices[fuel_name].toFixed(3) + "€/L </td>";
if (remark)
title += "<td class='thupdate'>" + remark + "</td>";
title += "</tr>";
}
title += "</tbody>"
title += "</table>"
for (var fuel_name in prices) {
var marker = L.marker(new L.LatLng(lat, lng), {title: city});
marker.bindPopup(title);
overlayMaps[fuel_name].addLayer(marker);
marker.prices = prices;
}
}
return overlayMaps;
}
// Background tiles
var tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
{
type: 'map',
maxZoom: 19,
minZoom: 2,
attribution: '© <a href="https://openstreetmap.org/copyright" rel="nofollow">OpenStreetMap</a> contributors, ' +
'<a href="https://creativecommons.org/licenses/by-sa/2.0/" rel="nofollow">CC-BY-SA</a>',
});
var map = L.map('map', {
center: L.latLng(46, 5.2),
zoom: 6,
layers: [tiles]
});
// Initialize the map
var fuel_list = ['E10', 'E85', 'GPLc', 'Gazole', 'SP95', 'SP98'];
var overlayMaps = initializeMarkers(map, fuel_list);
map.addLayer(overlayMaps["Gazole"]);
L.control.layers(overlayMaps, null, {position: 'topleft'}).addTo(map);
L.control.scale().addTo(map);
// The gas can be passed in the URI, check if we've got it
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
var selectedGas = JSON.parse(urlParams.get('gas'));
if (selectedGas in overlayMaps){
var currentLayer = findCurrentLayer(map, overlayMaps);
map.removeLayer(currentLayer);
map.addLayer(overlayMaps[selectedGas]);
}
var dynamic = false;
map.on('onCheckbox-dynamic', function() { dynamic = true; });
map.on('offCheckbox-dynamic', function() { dynamic = false; });
// Update the URI with the selected gas when the layer change, i.e. a different gas is selected
map.on('baselayerchange', function() {
var layerName = findCurrentLayer(map, overlayMaps).name;
var params = encodeURIComponent(JSON.stringify(layerName));
var newUrl = UpdateQueryString('gas', params);
history.pushState({}, null, newUrl);
});
map.on('load moveend baselayerchange onCheckbox-dynamic offCheckbox-dynamic', function() {
var layer = findCurrentLayer(map, overlayMaps);
colorizeMarkers(layer, dynamic, map.getBounds());
});
L.control.checkBox({name: 'dynamic', label: 'Colored dynamically'}).addTo(map);
</script>
</body>
</html>