-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite.js
109 lines (83 loc) · 2.66 KB
/
site.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
(function() {
//--- global variables
// keep track of map markers
var activeMarkers = [];
// raw mapping data
var data = [];
//--- end global variables
//--- mapping functions
function addMarker(row) {
var markerOptions = {'stroke': true, 'color': 'white', 'weight': 1, 'fillOpacity': 0.3,
'radius': 6, 'fillColor': '#004488', 'pane': 'markerPane'};
var latitude = row['lat'];
var longitude = row['lon'];
var marker = L.circleMarker([latitude, longitude], markerOptions).addTo(map);
if (row['description']) {
marker.bindPopup(row['description']);
marker.on('mouseover', function (e) {
this.openPopup();
});
}
activeMarkers.push(marker);
}
// delete all markers from the map
function removeMarkers() {
activeMarkers.forEach(function(marker) {
map.removeLayer(marker);
});
}
// replace the existing markers with a new set of markers
function replaceMarkers(data) {
removeMarkers();
data.forEach(function(row) {
addMarker(row);
});
}
// zoom the map to fit the existing set of active markers
function fitToMarkers() {
if (activeMarkers.length) {
var latlngs = activeMarkers.map(function(marker) {
return marker.getLatLng();
});
var bounds = L.latLngBounds(latlngs);
map.fitBounds(bounds, {padding: [50, 50]});
}
}
// reset the map to an overview of the contiguous US
function resetMapView(map) {
map.fitBounds([
[24.926294766395593, -68.29101562500001],
[49.26780455063753, -124.98046875000001]
]);
}
//--- end mapping functions
//--- called after loading this javascript file
jQuery(function($) {
// create map
map = L.map('map', {
scrollWheelZoom: false
});
// reset view
resetMapView(map);
// specify Carto tiles
L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}{r}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>, © <a href="https://carto.com/attribution" target="_blank">CARTO</a>'
}).addTo(map);
// load data, then add markers
$.ajax({
dataType: "json",
url: "data.json"
}).done(function(response) {
// store to global variable
data = response;
// add markers
data.forEach(function(location) {
addMarker(location);
});
// fit the map to the set of markers
fitToMarkers();
}).fail(function() {
console.log("Could not load data.json");
});
});
}).call(this);