-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
218 lines (183 loc) · 6.73 KB
/
index.html
File metadata and controls
218 lines (183 loc) · 6.73 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>OpenGridMap</title>
<meta
name="viewport"
content="initial-scale=1,maximum-scale=1,user-scalable=no"
/>
<link
href="https://api.mapbox.com/mapbox-gl-js/v2.11.0/mapbox-gl.css"
rel="stylesheet"
/>
<link href="styles.css" rel="stylesheet" />
<script src="https://api.mapbox.com/mapbox-gl-js/v2.11.0/mapbox-gl.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="map"></div>
<div id="menu">
<input id="dark-v11" type="radio" name="rtoggle" value="dark" />
<label for="dark-v11">dark</label>
<input
id="light-v11"
type="radio"
name="rtoggle"
value="light"
checked="checked"
/>
<label for="light-v11">light</label>
<input id="outdoors-v12" type="radio" name="rtoggle" value="outdoors" />
<label for="outdoors-v12">outdoors</label>
<input id="streets-v12" type="radio" name="rtoggle" value="streets" />
<label for="streets-v12">streets</label>
<input
id="satellite-streets-v12"
type="radio"
name="rtoggle"
value="satellite"
/>
<label for="satellite-streets-v12">satellite streets</label>
</div>
<script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v5.0.0/mapbox-gl-geocoder.min.js"></script>
<link
rel="stylesheet"
href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v5.0.0/mapbox-gl-geocoder.css"
type="text/css"
/>
<script>
mapboxgl.accessToken =
"pk.eyJ1IjoiemFib3AiLCJhIjoiY2xrNWdxd240MWFvMjNjcDFzcnBkdXRuciJ9.IxZ8EwRRSE1qzyoS56sPYw";
function getParamsFromHash() {
const hash = window.location.hash.substring(1);
const params = new URLSearchParams(hash);
return {
basemap: params.get("basemap") || "light-v11",
view: params.get("view") || "3.04/14.2/55.7",
};
}
const { basemap, view } = getParamsFromHash();
// Parse the view parameter into zoom, longitude, and latitude
const [zoom, lng, lat] = view.split("/").map(Number);
const map = new mapboxgl.Map({
container: "map",
style: `mapbox://styles/mapbox/${basemap}`,
zoom: zoom,
maxZoom: 17,
center: [lng, lat],
projection: "globe",
attributionControl: false,
}).addControl(
new mapboxgl.AttributionControl({
customAttribution:
'<a href="https://opengridmap.org/about">About OpenGridMap</a> | <a href="https://openinframap.org/">Open Infrastructure Map</a>',
})
);
function addLayersFromJSON() {
map.setFog({});
map.addSource("tileset", {
type: "vector",
tiles: ["https://openinframap.org/tiles/{z}/{x}/{y}.pbf"],
tileSize: 512,
});
map.addSource("mapbox-dem", {
type: "raster-dem",
url: "mapbox://mapbox.mapbox-terrain-dem-v1",
tileSize: 512,
maxzoom: 14,
});
map.setTerrain({ source: "mapbox-dem" });
axios
.get("layers.json")
.then((response) => {
const layers = response.data;
layers.forEach((layer) => {
map.addLayer(layer);
});
const popup = new mapboxgl.Popup({
closeButton: true,
closeOnClick: true,
});
function handleMapClick(e) {
map.getCanvas().style.cursor = "pointer";
const feature = e.features[0];
let popupContent = "";
for (const property in feature.properties) {
if (feature.properties[property] !== "") {
popupContent += `<strong>${property}:</strong> ${feature.properties[property]}<br>`;
}
}
popup.setLngLat(e.lngLat).setHTML(popupContent).addTo(map);
}
map.on("click", "all", handleMapClick);
map.on("click", "power_substation", handleMapClick);
map.on("mouseenter", "all", function () {
map.getCanvas().style.cursor = "pointer";
});
map.on("mouseenter", "power_substation", function () {
console.log("mouseenter");
map.getCanvas().style.cursor = "pointer";
});
map.on("mouseleave", "all", function () {
map.getCanvas().style.cursor = "";
});
map.on("mouseleave", "power_substation", function () {
map.getCanvas().style.cursor = "";
});
})
.catch((error) => {
console.error("Error loading layers.json:", error);
});
}
map.on("style.load", () => {
addLayersFromJSON();
});
const geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl,
placeholder: "Search for a location",
});
map.addControl(geocoder);
const layerList = document.getElementById("menu");
const inputs = layerList.getElementsByTagName("input");
function updateRadioButton(selectedBasemapId) {
for (const input of inputs) {
if (input.id === selectedBasemapId) {
input.checked = true;
break;
}
}
}
updateRadioButton(basemap);
for (const input of inputs) {
input.onclick = (layer) => {
const layerId = layer.target.id;
console.log("layerId", layerId);
map.setStyle("mapbox://styles/mapbox/" + layerId);
// Update the URL hash when the user selects a new basemap
const currentParams = new URLSearchParams(
window.location.hash.substring(1)
);
currentParams.set("basemap", layer.target.id); // Set the basemap to the selected radio button
window.location.hash = currentParams.toString(); // Update the URL hash
};
}
// Listen for the map moveend event and update the `view` parameter in the URL hash
map.on("moveend", function () {
const zoom = map.getZoom();
const center = map.getCenter();
const view = `${zoom.toFixed(2)}/${center.lng.toFixed(
6
)}/${center.lat.toFixed(6)}`;
// Update the URL hash with the new view value
const currentParams = new URLSearchParams(
window.location.hash.substring(1)
);
currentParams.set("view", view); // Update the view parameter in the URL hash
const updatedHash = `#${currentParams.toString().replace(/%2F/g, "/")}`;
window.location.hash = updatedHash;
});
</script>
</body>
</html>