-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
111 lines (87 loc) · 3.05 KB
/
script.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
104
105
106
107
108
109
110
111
let map = L.map('map', {
center: [42.1044, -75.9127],
zoom: 8,
minZoom: 8,
maxBounds: L.latLngBounds([40.8821, -80.0110], [43.3267, -71.8335]),
maxBoundsViscosity: 1
});
let overlayStyle = {
"color": "#dcae25",
"weight": 2,
"opacity": 0.5,
"fillColor": "#ffffff",
"fillOpacity": 0
};
let hoveredStyle = {
"color": "#ff0000",
"weight": 2,
"opacity": 0,
"fillColor": "#dcae25",
"fillOpacity": 1
};
async function main() {
// fetch the counties stored in Heart of NY database
const DB_COUNTIES = await fetch("http://localhost:3000/api/counties")
.then((res) => res.json())
.then((data) => data)
.catch(error => console.error('Error fetching data:', error));
// fetch the county societies stored in Heart of NY database
const DB_SOCIETIES = await fetch("http://localhost:3000/api/societies")
.then((res) => res.json())
.then((data) => data)
.catch(error => console.error('Error fetching data:', error));
// filter GeoJSON data to only include counties that are in the database
const FILTERED_COUNTIES_GEOJSON = COUNTIES_GEOJSON.features.filter((feature) => DB_COUNTIES.map((county) => county.county_name) .includes(feature.properties.name));
function onEachFeature(feature, layer) {
let name = L.tooltip({
content: feature.properties.name,
className: "label",
direction: "center",
opacity: 1
});
layer.bindTooltip(name);
function showInfo() {
const container = document.getElementById("info-container");
container.setAttribute("show", "true");
const close = document.getElementById("info-close");
close.onclick = () => hideInfo();
const county = DB_COUNTIES.find((county) => county.county_name === feature.properties.name);
const society = DB_SOCIETIES.find((society) => society.county_id === county.county_id);
const infoName = document.getElementById("info-name");
const infoSociety = document.getElementById("info-society");
infoName.innerHTML = county.county_name;
infoSociety.innerHTML = `<a href=${society.website}>${society.society_name}</a>`;
}
function hideInfo() {
const container = document.getElementById("info-container");
container.setAttribute("show", "false");
}
function onMouseOver(e) {
e.target.setStyle(hoveredStyle);
}
function onMouseOut(e) {
e.target.setStyle(overlayStyle);
}
function onMouseClick(layer) {
showInfo(layer);
}
layer.on({
mouseover: onMouseOver,
mouseout: onMouseOut,
click: onMouseClick
});
}
// add tile layer to map
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);
// add county overlay layer
L.geoJSON(FILTERED_COUNTIES_GEOJSON, {
style: overlayStyle,
onEachFeature: onEachFeature
}).addTo(map);
// add marker at TechWorks!
// L.marker([42.10442541105549, -75.91265528372267]).addTo(map).bindPopup('TechWorks!').openPopup();
}
main();