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
|
var map = L.map('map')//.setView([55.68, 12.57], 13);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);
let known_store_chains;
let icons = {};
var markers = L.featureGroup()
var polygons = L.featureGroup()
var heatmap = L.featureGroup()
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
if (!urlParams.has('markers') || urlParams.get('markers') == "1") {
markers.addTo(map);
}
if (!urlParams.has('polygons') || urlParams.get('polygons') == "1") {
polygons.addTo(map);
}
if (!urlParams.has('heatmap') || urlParams.get('heatmap') == "1") {
heatmap.addTo(map);
}
const CategoryField = document.getElementById('Category');
const CountryField = document.getElementById('Country');
const LoadingIndicator = document.getElementById('LoadingIndicator');
const SubmitButton = document.getElementById('SubmitButton');
const form = document.getElementById('QueryForm');
const heatmap_intensity_element = document.getElementById("HeatmapIntensity");
const heatmap_radius_element = document.getElementById("HeatmapRadius");
var shops;
const DefaultCategory = "shop:supermarket";
const DefaultCountry = "Denmark";
var Categories = []
var overlayMaps = {
"POI markers": markers,
"Nearest POI polygons": polygons,
"POI heatmap": heatmap
};
var layerControl = L.control.layers({},overlayMaps).addTo(map);
form.addEventListener('submit', (event) => {
event.preventDefault(); // Prevents the default form submission
if (SubmitButton.disabled) { // Disallow double queries
return;
}
SubmitButton.disabled = true;
LoadingIndicator.hidden = false;
const url = new URL(location);
url.searchParams.set("category", CategoryField.value);
url.searchParams.set("country", CountryField.value);
history.pushState({}, "", url);
markers.clearLayers();
polygons.clearLayers();
fetchAll(CountryField.value, CategoryField.value);
});
heatmap_intensity_element.addEventListener('input', (event) => {
changeHeatmap();
});
heatmap_radius_element.addEventListener('input', (event) => {
changeHeatmap();
});
function changeHeatmap() {
const url = new URL(location);
url.searchParams.set("HeatmapIntensity", heatmap_intensity_element.value);
history.pushState({}, "", url);
url.searchParams.set("HeatmapRadius", heatmap_radius_element.value);
history.pushState({}, "", url);
drawHeatmap();
}
function alterHeatMapParameter(event) {
event.srcElement.value;
}
async function fetchCategories() {
let response;
try {
response = await fetch("categories.json");
if (!response.ok) {
// If the response status is not OK, throw an error
throw new Error(`HTTP error! Status: ${response.status}`);
}
} catch (error) {
alert(`Failed to fetch categories\n${error.message}`)
}
categories = await response.json();
CategoryField.pattern = categories.join('|');
const CategoriesDataList = document.getElementById('CategoriesDataList');
categories.forEach(function(item){
var option = document.createElement('option');
option.value = item;
option.innerHTML = item;
CategoriesDataList.appendChild(option);
});
}
async function fetchChains() {
try {
const response = await fetch("chains.json");
known_store_chains = await response.json();
if (!response.ok) {
// If the response status is not OK, throw an error
throw new Error(`HTTP error! Status: ${response.status}`);
}
} catch (error) {
alert(`Failed to fetch chains\n${error.message}`)
}
icons["Unknown"] = L.icon({
iconUrl: 'icons/Unknown.png',
iconSize: [10, 16],
iconAnchor: [5, 8],
});
for (let known_store_chain in known_store_chains) {
icons[known_store_chain] = L.icon({
iconUrl: `icons/${known_store_chain}.png`,
iconAnchor: [8, 8],
});
}
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const category_url_param = urlParams.get('category')
const country_url_param = urlParams.get('country')
//console.log(country_url_param)
const category_field_value = CategoryField.value
const country_field_value = CountryField.value
// Retains saved value from input field on reload
if (category_url_param)
{
CategoryField.value = category_url_param;
} else if (category_field_value == "") {
CategoryField.value = DefaultCategory;
}
if (country_url_param)
{
CountryField.value = country_url_param;
} else if (country_field_value == "") {
CountryField.value = DefaultCountry;
}
fetchAll(CountryField.value, CategoryField.value);
}
function addPolygon(store) {
let color;
if (known_store_chains.hasOwnProperty(store.brand)) {
color = known_store_chains[store.brand];
} else {
color = 'grey';
}
L.polygon(store.polygon, {color: color}).addTo(polygons);
}
function addShop(shop) {
let icon;
if (known_store_chains.hasOwnProperty(shop.brand)){
icon = icons[shop.brand];
} else {
icon = icons["Unknown"];
}
L.marker([shop.lat, shop.long], {
title: shop.name,
icon: icon,
}).addTo(markers);
addPolygon(shop);
}
function drawHeatmap() {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const HeatmapIntensityUrlParam = urlParams.get("HeatmapIntensity")
if (HeatmapIntensityUrlParam){
heatmap_intensity_element.value = HeatmapIntensityUrlParam;
}
const HeatmapRadiusUrlParam = urlParams.get("HeatmapRadius")
if (HeatmapRadiusUrlParam){
heatmap_radius_element.value = HeatmapRadiusUrlParam;
}
heatmap.clearLayers();
const shops_heatmap_format = shops.map(r => [r.lat, r.long, parseInt(heatmap_intensity_element.value)]); // Convert data to heatmap format
var heat = L.heatLayer(shops_heatmap_format, {radius: parseInt(heatmap_radius_element.value)}).addTo(heatmap);
}
async function fetchAll(country, category) {
if (CountryField.value == "Denmark") {
console.log('Focusing Copenhagen')
map.setView([55.68, 12.57], 13);
} else if(CountryField.value == "Germany") {
console.log('Focusing Hamburg')
map.setView([53.56, 10.01], 12);
}
let response;
try {
response = await fetch(`all.json?country=${country}&category=${category}`)
if (!response.ok) {
// If the response status is not OK, throw an error
throw new Error(`HTTP error! Status: ${response.status}`);
}
} catch (error) {
alert(`Failed to fetch data\n${error.message}`)
}
shops = await response.json();
shops.forEach((shop) => addShop(shop)); // Add icons and polygons
drawHeatmap();
LoadingIndicator.hidden = true;
SubmitButton.disabled = false;
}
fetchChains();
fetchCategories();
|