-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
422 lines (358 loc) · 11.4 KB
/
map.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
var map;
var parkInfo;
var poiLayer = L.layerGroup();
var trailsLayer = L.layerGroup();
var showDistancesAtZoom = 17; // can be set by parkInfo
var labelsVisible = true;
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(
/[?&]+([^=&]+)=([^&]*)/gi,
function (m, key, value) {
vars[key] = value;
}
);
return vars;
}
function listParks() {
// TODO: replace with a map
var listHTML = "";
listHTML +=
"<h1>Map markers</h1><table><thead><th>Icon</th><th>Name</th><th>Geojson.io name</th><th>Description</th></thead><tbody>";
for (const property in markerList) {
const icon = markerList[property];
listHTML += `<tr><td><img src="${icon.marker.options.iconUrl}"></td><td>${icon.name}</td><td>${icon.maki}</td><td>${icon.description}</td></tr>`;
}
listHTML += "</tbody></table><br><br>";
listHTML +=
"<h1>Default trail markings</h1><table><thead><th>Example</th><th>Type</th><th>Color</th><th>Width</th></thead><tbody>";
defaultLegend.forEach((item) => {
listHTML += `<tr><th class="legend" style="box-shadow: none">${getLegendLine(
item
)}</th><th>${item.name}</th><th>${item.color}</th><th>${
item.width
}</th></tr>`;
});
listHTML += "</tbody></table>";
listHTML += "<br><br><h1>Maps</h1>";
list.forEach((park) => {
listHTML += `<a href="?park=${park.id}">${park.name}</a><br>`;
});
document.getElementById("map").innerHTML = listHTML;
}
function addFullScreenControl(fullScreen, parkInfo) {
var fullScreenControl = L.control({
position: "topright",
});
fullScreenControl.onAdd = function () {
var div = L.DomUtil.create(
"div",
"leaflet-control-layers leaflet-control full-screen-control"
);
var url = "";
if (fullScreen === "off") {
var oldURL = window.location.href;
url = oldURL.replace("full-screen=off", "full-screen=on");
}
if (fullScreen === "on") {
url = `https://atlnature.com/blog/${parkInfo.id}#park-map`;
}
div.innerHTML += `<a href="${url}" target="_top"><img src="${
fullScreen === "on" ? "compress" : "expand"
}.png"></a>`;
return div;
};
fullScreenControl.addTo(map);
}
function getLegendLine(legendItem) {
var topMargin = 9 - legendItem.width / 2;
var dashArray = legendItem.style === "stairs" ? "dashed" : "solid";
return `<div class="line" style="display: inline-block; border-bottom-color: ${legendItem.color}; border-bottom-width: ${legendItem.width}px; margin-top: ${topMargin}px; border-bottom-style: ${dashArray};"></div> `;
}
function addLegend(legendItems) {
if (legendItems) {
var legend = L.control({
position: "bottomright",
});
legend.onAdd = function (map) {
var div = L.DomUtil.create("div", "legend");
div.innerHTML += "<h4>Trails:</h4>";
for (var i = 0; i < legendItems.length; i++) {
var legendItem = legendItems[i];
div.innerHTML += getLegendLine(legendItem) + `${legendItem.name}<br>`;
}
return div;
};
legend.addTo(map);
}
}
function requestLocation() {
if (map) {
map
.locate({
watch: true,
setView: false,
})
.on("locationfound", onLocationFound);
}
}
function drawMap(file) {
const mapSettings = {
maxZoom: 18,
minZoom: 10,
attribution:
'© <a href="https://apps.mapbox.com/feedback/">Mapbox</a> © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
};
function constructTileURL(name) {
return `https://api.mapbox.com/styles/v1/mapbox/${name}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwc2FuZGFwcHMiLCJhIjoiY2pjYmNncHo2MHA2djJ3cW8ycWNzZHBwOSJ9.9c-NcWYyKKsbvtHF9tuQBA`;
}
map = L.map("map", {
center: [33.753, -84.3984],
zoom: 11,
dragging: !L.Browser.mobile,
});
var outdoors = L.tileLayer(
constructTileURL("outdoors-v11"),
mapSettings
).addTo(map);
var streets = L.tileLayer(constructTileURL("streets-v11"), mapSettings);
var satellite = L.tileLayer(constructTileURL("satellite-v9"), mapSettings);
var satelliteStreets = L.tileLayer(
constructTileURL("satellite-streets-v11"),
mapSettings
);
var tileLayers = {
Outdoors: outdoors,
Streets: streets,
Satellite: satellite,
"Satellite + Streets": satelliteStreets,
};
var dataLayers = {
"Points of Interest": poiLayer,
Trails: trailsLayer,
};
L.control.layers(tileLayers, dataLayers).addTo(map);
poiLayer.addTo(map);
trailsLayer.addTo(map);
var bounds = L.geoJSON(file).getBounds();
map.fitBounds(bounds);
trailsLayer.eachLayer((layer) => {
var tooltip = layer.getTooltip();
if (tooltip) {
layer.openTooltip();
}
});
poiLayer.eachLayer((layer) => {
var tooltip = layer.getTooltip();
if (tooltip) {
layer.openTooltip();
}
});
var geolocationControl = L.Control.extend({
onAdd: function () {
var div = L.DomUtil.create(
"div",
"leaflet-control-layers leaflet-control geolocation-control"
);
div.innerHTML += `<span><img src="geolocation.png" onclick="requestLocation()"></span>`;
return div;
},
onRemove: function () {},
});
L.control.geolocation = function () {
return new geolocationControl();
};
L.control.geolocation({ position: "topright" }).addTo(map);
map.on("zoomend", showOrHideDistanceLabels);
showOrHideDistanceLabels();
}
function mapPark(parkID, fullScreen) {
parkInfo = list.find((park) => {
return park.id === parkID;
});
showDistancesAtZoom = parkInfo.showDistancesAtZoom;
fetch(`./geojsons/${parkID}.geojson`)
.then((resp) => resp.json())
.then((response) => {
processGeojson(response);
drawMap(response);
addLegend(parkInfo.legend);
if (fullScreen === "off" || fullScreen === "on") {
addFullScreenControl(fullScreen, parkInfo);
}
});
}
var geolocationDot;
var geolocationAccuracy;
function onLocationFound(e) {
console.log("location found");
if (geolocationDot) {
geolocationDot.removeFrom(map);
geolocationAccuracy.removeFrom(map);
}
var radius = e.accuracy / 2;
geolocationDot = L.circleMarker(e.latlng, {
radius: 5,
stroke: false,
fillOpacity: 1.0,
}).addTo(map);
geolocationAccuracy = L.circle(e.latlng, radius, {
stroke: false,
}).addTo(map);
}
function processLineString(feature) {
var line = L.geoJSON(feature, {
color: feature.properties.stroke,
weight: feature.properties["stroke-width"],
dashArray: feature.properties.style === "stairs" ? "8" : "none",
});
var popupText = feature.properties.name ? feature.properties.name : ""; // TODO: add .surface, etc.
if (popupText && feature.properties.miles) popupText += ", ";
if (feature.properties.miles)
popupText += feature.properties.miles.toString();
if (feature.properties.miles)
popupText += feature.properties.miles == 1 ? " mile" : " miles";
line.addTo(trailsLayer);
if (popupText) line.bindTooltip(popupText);
}
function reverseCoordinateArray(coordinates) {
return [coordinates[1], coordinates[0]];
}
function addGMapsLinkToPoint(marker) {
// TODO: only add these if the properties['navigate-to'] is true (true is a string, for now)
// TODO: should these be geo: links?
return `<a href="https://www.google.com/maps/place/${
marker.getLatLng().lat
},${marker.getLatLng().lng}" target="_blank">Directions to point</a>`;
}
function createPopup(feature, marker) {
const symbol = feature.properties["marker-symbol"];
let popupContent = "";
if (markerList[symbol] && markerList[symbol].name) {
if (markerList[symbol].name === "Bike Parking" && feature.properties.name) {
// no-op
} else {
popupContent += markerList[symbol].name + "<br>";
}
}
if (feature.properties.name) {
popupContent += `${feature.properties.name}<br>`;
}
if (feature.properties.trailhead) {
popupContent += `<a href="${feature.properties.trailhead}">Photo of trailhead</a><br>`;
}
if (markerList[symbol] && markerList[symbol].linkOut) {
popupContent += addGMapsLinkToPoint(marker);
} else {
// TODO: possibly display a popup if the marker isn't a photo or navigable point
}
marker.bindPopup(popupContent);
}
function getTextLabelOffset(feature) {
const DEFAULT_OFFSET = [0, -29];
const OFFSET_ADJUSTMENT = 12;
const ANGLE_ADJUSTMENT = Math.round(OFFSET_ADJUSTMENT * 0.75);
var offset = DEFAULT_OFFSET;
if (feature.properties.offset) {
const angle = feature.properties.offset.length === 2 ? true : false;
const adjustment = angle ? ANGLE_ADJUSTMENT : OFFSET_ADJUSTMENT;
var firstLetter = feature.properties.offset.charAt(0).toLowerCase();
var lastLetter = feature.properties.offset.substr(-1).toLowerCase();
if (firstLetter === "n") {
offset[1] = offset[1] + adjustment;
} else if (firstLetter === "s") {
offset[1] = offset[1] - adjustment;
}
if (lastLetter === "e") {
offset[0] = offset[0] - adjustment;
} else if (lastLetter === "w") {
offset[0] = offset[0] + adjustment;
}
}
return offset;
}
function processPoint(feature) {
const symbol = feature.properties["marker-symbol"];
var marker;
if (symbol === "miles") {
var miles =
feature.properties.miles ||
Math.round(feature.properties.yards / 176) / 10;
if (miles !== 0) {
marker = L.marker(reverseCoordinateArray(feature.geometry.coordinates), {
opacity: 0,
});
marker.bindTooltip(miles.toString(), {
className: "distance-label",
direction: "center",
offset: getTextLabelOffset(feature),
pane: "tilePane",
permanent: true,
});
}
} else if (markerList[symbol]) {
if (markerList[symbol].marker) {
marker = L.marker(reverseCoordinateArray(feature.geometry.coordinates), {
icon: markerList[symbol].marker,
});
}
createPopup(feature, marker);
} else {
marker = L.marker(reverseCoordinateArray(feature.geometry.coordinates));
}
if (marker) {
if (symbol === "cross" || symbol === "miles") {
marker.addTo(trailsLayer);
} else {
marker.addTo(poiLayer);
}
}
}
function processPolygon(feature) {
var polygon = L.geoJSON(feature, {
fillColor: feature.properties.fill,
fillOpacity: feature.properties["fill-opacity"],
color: feature.properties.stroke,
weight: feature.properties["stroke-width"],
opacity: feature.properties["stroke-opacity"],
});
if (feature.properties.name) polygon.bindTooltip(feature.properties.name);
polygon.addTo(poiLayer);
}
function processGeojson(file) {
file.features.forEach((feature) => {
if (feature.geometry.type === "LineString") {
processLineString(feature);
} else if (feature.geometry.type === "Point") {
processPoint(feature);
} else if (
feature.geometry.type === "Polygon" ||
feature.geometry.type === "MultiPolygon"
) {
processPolygon(feature);
}
});
}
window.onload = () => {
var parkID = getUrlVars()["park"];
var fullScreen = getUrlVars()["full-screen"];
if (parkID) {
mapPark(parkID, fullScreen);
} else {
listParks();
}
};
function showOrHideDistanceLabels() {
var currentZoom = map.getZoom();
if (labelsVisible && currentZoom < showDistancesAtZoom) {
labelsVisible = false;
trailsLayer.eachLayer((marker) => {
marker.closeTooltip();
});
} else if (!labelsVisible && currentZoom >= showDistancesAtZoom) {
labelsVisible = true;
trailsLayer.eachLayer((marker) => {
marker.openTooltip();
});
}
}