-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
103 lines (94 loc) · 3.29 KB
/
app.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
var map = L.map('map').setView([57.1477, -2.095], 15);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
maxZoom: 19,
}).addTo(map);
var restaurantsMarkers = [];
var cafesMarkers = [];
var attractionsMarkers = [];
var barsMarkers = []; // You had bars in your HTML, so I added it here too.
function fetchDataAndPlaceMarkers(category, targetArray) {
var query;
switch (category) {
case 'restaurants':
query = `
[out:json][timeout:25];
(
node["amenity"="restaurant"](around:1000,57.1477,-2.095);
way["amenity"="restaurant"](around:1000,57.1477,-2.095);
);
out body;
>;
out skel qt;
`;
break;
case 'cafes':
query = `
[out:json][timeout:25];
(
node["amenity"="cafe"](around:1000,57.1477,-2.095);
way["amenity"="cafe"](around:1000,57.1477,-2.095);
);
out body;
>;
out skel qt;
`;
break;
case 'attractions':
query = `
[out:json][timeout:25];
(
node["tourism"](around:1000,57.1477,-2.095);
way["tourism"](around:1000,57.1477,-2.095);
);
out body;
>;
out skel qt;
`;
break;
case 'bars':
query = `
[out:json][timeout:25];
(
node["amenity"="bar"](around:1000,57.1477,-2.095);
way["amenity"="bar"](around:1000,57.1477,-2.095);
);
out body;
>;
out skel qt;
`;
break;
}
var overpassURL = "https://overpass-api.de/api/interpreter";
$.post(overpassURL, {
data: query
}, function(data) {
data.elements.forEach(element => {
if (element.type === "node") {
var marker = L.marker([element.lat, element.lon]).bindPopup(element.tags.name || category);
targetArray.push(marker);
marker.addTo(map);
}
});
});
}
function toggleMarkers(category) {
if ($('#' + category).prop('checked')) {
for (let i = 0; i < window[category + 'Markers'].length; i++) {
window[category + 'Markers'][i].addTo(map);
}
} else {
for (let i = 0; i < window[category + 'Markers'].length; i++) {
map.removeLayer(window[category + 'Markers'][i]);
}
}
}
$('#restaurants').change(() => { toggleMarkers('restaurants'); });
$('#cafes').change(() => { toggleMarkers('cafes'); });
$('#attractions').change(() => { toggleMarkers('attractions'); });
$('#bars').change(() => { toggleMarkers('bars'); });
// Initial fetch from Overpass and placement of markers
fetchDataAndPlaceMarkers('restaurants', restaurantsMarkers);
fetchDataAndPlaceMarkers('cafes', cafesMarkers);
fetchDataAndPlaceMarkers('attractions', attractionsMarkers);
fetchDataAndPlaceMarkers('bars', barsMarkers);